-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathredis_adapter.py
427 lines (366 loc) · 16.3 KB
/
redis_adapter.py
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# GSC-19165-1, "The On-Board Artificial Intelligence Research (OnAIR) Platform"
#
# Copyright © 2023 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# No copyright is claimed in the United States under Title 17, U.S. Code.
# All Other Rights Reserved.
#
# Licensed under the NASA Open Source Agreement version 1.3
# See "NOSA GSC-19165-1 OnAIR.pdf"
"""redis_adapter module
This module contains a DataSource class, which serves as a data source
for sim.py by receiving messages from one or more REDIS servers. It
implements the OnAirDataSource interface and provides functionality for
connecting to REDIS servers, subscribing to channels, and processing
incoming data.
The module utilizes Redis, threading, and JSON libraries to handle
server connections and data parsing.
"""
import threading
import time
import json
import redis
from onair.data_handling.on_air_data_source import OnAirDataSource
from onair.data_handling.on_air_data_source import ConfigKeyError
from onair.data_handling.tlm_json_parser import parseJson
from onair.src.util.print_io import print_msg
from onair.data_handling.parser_util import extract_meta_data_handle_ss_breakdown
class DataSource(OnAirDataSource):
"""Implements OnAirDataSource interface for receiving data from REDIS servers.
This class provides the following functionality:
- Establishes connections to one or more REDIS servers
- Subscribes to specified channels on each server
- Listens for incoming messages on subscribed channels
- Parses and processes received data
- Provides methods to access the latest data received
The class uses double-buffering for thread-safe access to recent data.
"""
def __init__(self, data_file, meta_file, ss_breakdown=False):
"""Initialize the DataSource object.
Parameters
----------
data_file : str
Path to the data file (not used in Redis adapter).
meta_file : str
Path to the metadata file containing Redis server configurations.
ss_breakdown : bool, optional
Flag to indicate whether to handle subsystem breakdown, by default False.
Flag to indicate whether to handle subsystem breakdown, by default
False.
Notes
-----
This method performs the following tasks:
1. Initializes the parent class.
2. Sets up threading lock and new data flag.
3. Initializes lists for servers and current data.
4. Creates a double buffer for current data storage.
5. Connects to Redis servers specified in the metadata file.
"""
super().__init__(data_file, meta_file, ss_breakdown)
self.new_data_lock = threading.Lock()
self.new_data = False
self.servers = []
self.current_data = []
self.current_data.append(
{"headers": self.order, "data": list("-" * len(self.order))}
)
self.current_data.append(
{"headers": self.order, "data": list("-" * len(self.order))}
)
self.double_buffer_read_index = 0
self.connect()
def connect(self):
"""Connect to Redis servers and set up subscriptions.
This method iterates through the server configurations, attempts to
connect to each Redis server, and sets up subscriptions for the
specified channels.
For each server configuration:
1. Extracts connection details (address, port, db, password).
2. Creates a Redis connection if subscriptions are specified.
3. Pings the server to ensure connectivity.
4. Sets up a pubsub object and subscribes to specified channels.
5. Starts a listener thread for each server connection.
When a connection fails, an error message is output, and the method
continues to the next server configuration.
"""
print_msg("Redis adapter connecting to server...")
for idx, server_config in enumerate(self.server_configs):
address = server_config.get("address", "localhost")
port = server_config.get("port", 6379)
db = server_config.get("db", 0)
password = server_config.get("password", "")
# if there are subscriptions in this Redis server configuration's subscription key
if len(server_config["subscriptions"]) != 0:
# Create the servers and append them to self.servers list
self.servers.append(redis.Redis(address, port, db, password))
try:
# Ping server to make sure we can connect
self.servers[-1].ping()
print_msg(f"... connected to server # {idx}!")
# Set up Redis pubsub function for the current server
pubsub = self.servers[-1].pubsub()
for s in server_config["subscriptions"]:
pubsub.subscribe(s)
print_msg(f"Subscribing to channel: {s} on server # {idx}")
listen_thread = threading.Thread(
target=self.message_listener, args=(pubsub,)
)
listen_thread.start()
# This except will be hit if self.servers[-1].ping()
# threw an exception (could not properly ping server)
except (
redis.exceptions.ConnectionError,
redis.exceptions.TimeoutError,
) as e:
error_type = type(e).__name__
error_message = str(e)
print_msg(
f"Did not connect to server # {idx} due to {error_type}: {error_message}"
f"\nNot setting up subscriptions.",
["FAIL"],
)
else:
print_msg("No subscriptions given! Redis server not created")
def parse_meta_data_file(self, meta_data_file, ss_breakdown):
"""
Parse the metadata file and extract configuration information.
Parameters
----------
meta_data_file : str
Path to the metadata file.
ss_breakdown : bool
Flag to indicate whether to handle subsystem breakdown.
Returns
-------
dict
Extracted configuration information.
Raises
------
ConfigKeyError
If required keys are missing in the metadata file.
Notes
-----
This method performs the following tasks:
1. Extracts metadata and handles subsystem breakdown.
2. Parses the JSON content of the metadata file.
3. Validates and extracts Redis server configurations.
4. Extracts the 'order' key from the metadata.
The extracted Redis server configurations are stored in
`self.server_configs`.
The 'order' key is stored in `self.order`.
"""
self.server_configs = []
configs = extract_meta_data_handle_ss_breakdown(meta_data_file, ss_breakdown)
meta = parseJson(meta_data_file)
keys = meta.keys()
# Setup redis server configuration
# Checking if 'redis' exists
if "redis" in keys:
count_server_config = 0
# Checking if dictionaries within 'redis' key each have a 'subscription' key.
for server_config in meta["redis"]:
redis_config_keys = server_config.keys()
if not "subscriptions" in redis_config_keys:
raise ConfigKeyError(
f"Config file: '{meta_data_file}' "
f"missing required key 'subscriptions' from {count_server_config}"
+ " in key 'redis'"
)
count_server_config += 1
# Saving all of Redis dictionaries from JSON file to self.server_configs
self.server_configs = meta["redis"]
if "order" in keys:
self.order = meta["order"]
else:
raise ConfigKeyError(
f"Config file: '{meta_data_file}' " "missing required key 'order'"
)
return configs
def process_data_file(self, data_file):
"""Process the data file (not used in Redis Adapter).
This method is not used in the Redis Adapter and simply prints a
message indicating that the file is being ignored.
Parameters
----------
data_file : str
Path to the data file (not used).
Notes
-----
Data is received through Redis subscriptions rather than from a file,
so this method does not perform any actual processing.
"""
print("Redis Adapter ignoring file")
def get_vehicle_metadata(self):
"""Get the vehicle metadata for headers and test assignments.
Returns
-------
tuple
A tuple containing two elements:
- all_headers : list
A list of all headers for the vehicle data.
- test_assignments : dict
A dictionary containing the test assignments from the binning
configurations.
Notes
-----
This method returns the metadata necessary for processing and organizing
vehicle data. The all_headers list provides information about the
structure of the data, while the test_assignments dictionary contains
information about how tests are assigned or grouped.
"""
return self.all_headers, self.binning_configs["test_assignments"]
def get_next(self):
"""Retrieve the next available data from the buffer.
This method waits for new data to become available, then returns it.
It uses a double-buffering technique to ensure thread-safe access to
the data.
Returns
-------
list
The latest data retrieved from the buffer.
Notes
-----
This method blocks until new data is available. It uses a short sleep
(10 milliseconds) between checks to reduce CPU usage while waiting.
The double-buffering technique involves:
1. Waiting for the `new_data` flag to become True.
2. Resetting the `new_data` flag to False.
3. Updating the read index for the double buffer.
4. Returning the data from the current read buffer.
This approach allows one buffer to be read while the other is being
written to, ensuring data consistency and thread safety.
"""
data_available = False
while not data_available:
with self.new_data_lock:
data_available = self.has_data()
if not data_available:
time.sleep(0.01)
read_index = 0
with self.new_data_lock:
self.new_data = False
self.double_buffer_read_index = (self.double_buffer_read_index + 1) % 2
read_index = self.double_buffer_read_index
return self.current_data[read_index]["data"]
def has_more(self):
"""Check if more data is available.
This method always returns True for the Redis adapter, as it
continuously listens for new messages.
Returns
-------
bool
Always True, indicating that more data can potentially be received.
Notes
-----
This method is part of the OnAirDataSource interface. For the Redis
adapter, it always returns True because the adapter continuously
listens for new messages from the subscribed Redis channels.
"""
return True
def message_listener(self, pubsub):
"""Listen for messages from Redis pubsub and process them.
This method continuously listens for messages from the Redis pubsub
connection, processes JSON messages, updates the current data buffer,
handling various error conditions.
Parameters
----------
pubsub : redis.client.PubSub
A Redis pubsub object for subscribing to channels and receiving
messages.
Notes
-----
The method performs the following tasks:
1. Listens for messages from the pubsub connection.
2. Processes messages of type "message".
3. Attempts to parse the message data as JSON.
4. Updates the current data buffer with new values.
5. Handles missing or unexpected keys in the message data.
6. Sets a flag to indicate new data is available.
7. Warns about non-message type receipts.
The method continues to run until the pubsub connection is closed or
an error occurs. If the listener loop exits, a warning is issued.
Warnings
--------
Warnings are issued for the following conditions:
- Non-JSON conforming message data
- Unexpected keys in the message data
- Expected keys missing from the message data
- Non-message type receipts
- Listener loop exit
"""
for message in pubsub.listen():
if message["type"] == "message":
channel_name = f"{message['channel'].decode()}"
# Attempt to load message as json
try:
data = json.loads(message["data"])
except ValueError:
# Warn of non-json conforming channel data received
non_json_msg = (
f"Subscribed channel `{channel_name}' "
"message received but is not in json "
f'format.\nMessage:\n{message["data"]}'
)
print_msg(non_json_msg, ["WARNING"])
continue
# Select the current data
current_data = self.current_data[
(self.double_buffer_read_index + 1) % 2
]
# turn all data points to unknown
current_data["data"] = ["-" for _ in current_data["data"]]
# Find expected keys for received channel
expected_message_keys = [
k for k in current_data["headers"] if channel_name in k
]
# Time is an expected key for all channels
expected_message_keys.append("time")
# Parse through the message keys for data points
for key in list(data.keys()):
if key.lower() == "time":
header_string = key.lower()
else:
header_string = f"{channel_name}.{key}"
# Look for channel specific values
try:
index = current_data["headers"].index(header_string)
current_data["data"][index] = data[key]
expected_message_keys.remove(header_string)
# Unexpected key in data
except ValueError:
# warn user about key in data that is not in header
print_msg(
f"Unused key `{key}' in message "
f"from channel `{channel_name}.'",
["WARNING"],
)
with self.new_data_lock:
self.new_data = True
# Warn user about expected keys missing from received data
for k in expected_message_keys:
print_msg(
f"Message from channel `{channel_name}' "
f"did not contain `{k}' key\nMessage:\n"
f"{data}",
["WARNING"],
)
else:
# Warn user about non message receipts
print_msg(
f"Redis adapter: channel "
f"'{message['channel'].decode()}' received "
f"message type: {message['type']}.",
["WARNING"],
)
# When listener loop exits warn user
print_msg("Redis subscription listener exited.", ["WARNING"])
def has_data(self):
"""
Check if new data is available.
Returns
-------
bool
The value of the new_data flag, which should be True if new data is
available, False otherwise.
"""
return self.new_data