Skip to content

Commit 0e072be

Browse files
committed
big cleanup. no request_did, no prefix
1 parent b9fc72d commit 0e072be

File tree

6 files changed

+20
-108
lines changed

6 files changed

+20
-108
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ Create an async callback method that `yield`s file info dictionaries. For exampl
2828
The arguments to the method are straight forward:
2929

3030
* `did_name`: the name of the DID that you should look up. It has the schema stripped off (e.g. if the user sent ServiceX `rucio://dataset_name_in_rucio`, then `did_name` will be `dataset_name_in_rucio`)
31-
* `info` contains a dict of various info about the request that asked for this DID:
32-
* `request-id` The request id that has this DID associated. For logging.
31+
* `info` contains a dict of various info about the request that asked for this DID.
3332

3433
Yield the results as you find them - ServiceX will actually start processing the files before your DID lookup is finished if you do this. The fields you need to pass back to the library are as follows:
3534

@@ -114,7 +113,7 @@ In the end, all DID finders for ServiceX will run under Kubernetes. ServiceX com
114113
__log = logger.getLogger(__name__)
115114
async def my_callback(did_name: str, info: Dict[str, Any]):
116115
__log.info(f'Looking up dataset {did_name}.',
117-
extra={'requestId': info['request-id']})
116+
extra={'somethign': info['something']})
118117

119118
for i in range(0, 10):
120119
yield {
@@ -125,9 +124,7 @@ In the end, all DID finders for ServiceX will run under Kubernetes. ServiceX com
125124
}
126125
```
127126

128-
Note the parameter `request-id`: this marks the log messages with the request id that triggered this DID request. This will enable the system to track all log messages across all containers connected with this particular request id - making debugging a lot easier.
129-
130-
The `start_did_finder` will configure the python root logger properly to dump messages with a request ID in them.
127+
The `start_did_finder` will configure the python root logger properly.
131128

132129
## URI Format
133130

src/servicex_did_finder_lib/communication.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ async def run_file_fetch_loop(
9191
# If we've been holding onto any files, we need to send them now.
9292
acc.send_on(did_info.file_count)
9393

94-
# Simple error checking and reporting
95-
if summary.file_count == 0:
96-
servicex.post_status_update(
97-
f"DID Finder found zero files for dataset {did}", severity="fatal"
98-
)
99-
10094
elapsed_time = int((datetime.now() - start_time).total_seconds())
10195
servicex.put_fileset_complete(
10296
{
@@ -107,11 +101,10 @@ async def run_file_fetch_loop(
107101
"elapsed-time": elapsed_time,
108102
}
109103
)
110-
servicex.post_status_update(f"Completed load of files in {elapsed_time} seconds")
111104

112105

113106
def rabbit_mq_callback(
114-
user_callback: UserDIDHandler, channel, method, properties, body, file_prefix=None
107+
user_callback: UserDIDHandler, channel, method, properties, body
115108
):
116109
"""rabbit_mq_callback Respond to RabbitMQ Message
117110
@@ -125,25 +118,20 @@ def rabbit_mq_callback(
125118
method ([type]): Delivery method
126119
properties ([type]): Properties of the message
127120
body ([type]): The body (json for us) of the message
128-
file_prefix([str]): Prefix to put in front of file paths to enable use of Cache service
129121
"""
130122
dataset_id = None # set this in case we get an exception while loading request
131-
request_id = None
132123
try:
133124
# Unpack the message. Really bad if we fail up here!
134125
did_request = json.loads(body)
135126
did = did_request["did"]
136127
dataset_id = did_request["dataset_id"]
137-
request_id = did_request["request_id"]
138128
endpoint = did_request["endpoint"]
139129
__logging.info(
140130
f"Received DID request {did_request}",
141-
extra={"request_id": request_id, "dataset_id": dataset_id}
131+
extra={"dataset_id": dataset_id}
142132
)
143-
servicex = ServiceXAdapter(request_id=request_id,
144-
dataset_id=dataset_id,
145-
endpoint=endpoint,
146-
file_prefix=file_prefix)
133+
servicex = ServiceXAdapter(dataset_id=dataset_id,
134+
endpoint=endpoint)
147135

148136
info = {
149137
"dataset-id": dataset_id,
@@ -155,11 +143,7 @@ def rabbit_mq_callback(
155143

156144
except Exception as e:
157145
_, exec_value, _ = sys.exc_info()
158-
__logging.exception("DID Request Failed", extra={"dataset_id": dataset_id})
159-
servicex.post_status_update(
160-
f"DID Request Failed for id {dataset_id}: " f"{str(e)} - {exec_value}",
161-
severity="fatal",
162-
)
146+
__logging.exception(f"DID Request Failed {str(e)}", extra={"dataset_id": dataset_id})
163147
raise
164148

165149
except Exception as e:
@@ -177,7 +161,6 @@ def init_rabbit_mq(
177161
queue_name: str,
178162
retries: int,
179163
retry_interval: float,
180-
file_prefix: str = None,
181164
): # type: ignore
182165
rabbitmq = None
183166
retry_count = 0
@@ -194,7 +177,7 @@ def init_rabbit_mq(
194177
queue=queue_name,
195178
auto_ack=False,
196179
on_message_callback=lambda c, m, p, b: rabbit_mq_callback(
197-
user_callback, c, m, p, b, file_prefix
180+
user_callback, c, m, p, b
198181
),
199182
)
200183
_channel.start_consuming()

src/servicex_did_finder_lib/did_logging.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
class DIDFormatter(logging.Formatter):
66
"""
7-
Need a customer formatter to allow for logging with request ids that vary.
8-
Normally log messages are "level instance component request_id msg" and
9-
request_id gets set by initialize_logging but we need a handler that'll let
10-
us pass in the request id and have that embedded in the log message
7+
Need a customer formatter to allow for logging with dataset ids that vary.
8+
Normally log messages are "level instance component dataset_id msg" and
9+
dataset_id gets set by initialize_logging but we need a handler that'll let
10+
us pass in the dataset id and have that embedded in the log message
1111
"""
1212

1313
def format(self, record: logging.LogRecord) -> str:
@@ -18,10 +18,10 @@ def format(self, record: logging.LogRecord) -> str:
1818
:return: formatted log message
1919
"""
2020

21-
if hasattr(record, "requestId"):
21+
if hasattr(record, "datasetId"):
2222
return super().format(record)
2323
else:
24-
setattr(record, "requestId", None)
24+
setattr(record, "datasetId", None)
2525
return super().format(record)
2626

2727

@@ -36,7 +36,7 @@ def initialize_root_logger(did_scheme: str):
3636
instance = os.environ.get('INSTANCE_NAME', 'Unknown')
3737
formatter = DIDFormatter('%(levelname)s ' +
3838
f"{instance} {did_scheme}_did_finder " +
39-
'%(requestId)s %(message)s')
39+
'%(datasetId)s %(message)s')
4040
handler = logging.StreamHandler()
4141
handler.setFormatter(formatter)
4242
handler.setLevel(logging.INFO)

src/servicex_did_finder_lib/servicex_adaptor.py

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,45 +35,17 @@
3535

3636

3737
class ServiceXAdapter:
38-
def __init__(self, endpoint, dataset_id, request_id=None, file_prefix=None):
38+
def __init__(self, endpoint, dataset_id):
3939
self.endpoint = endpoint
40-
self.request_id = request_id
4140
self.dataset_id = dataset_id
42-
self.file_prefix = file_prefix
4341

4442
self.logger = logging.getLogger(__name__)
4543
self.logger.addHandler(logging.NullHandler())
4644

47-
# TODO - remove. did finder should not know about request_id and call any
48-
# endpoint with request_id.
49-
def post_status_update(self, status_msg, severity="info"):
50-
success = False
51-
attempts = 0
52-
while not success and attempts < MAX_RETRIES:
53-
try:
54-
requests.post(f"{self.endpoint}{self.request_id}/status", data={
55-
"timestamp": datetime.now().isoformat(),
56-
"source": "DID Finder",
57-
"severity": severity,
58-
"info": status_msg
59-
})
60-
success = True
61-
except requests.exceptions.ConnectionError:
62-
self.logger.exception(f'Connection error to ServiceX App. Will retry '
63-
f'(try {attempts} out of {MAX_RETRIES}')
64-
attempts += 1
65-
if not success:
66-
self.logger.error(f'After {attempts} tries, failed to send ServiceX App a status '
67-
f'message: {str(status_msg)} - Ignoring error.')
68-
69-
# TODO - remove
70-
def _prefix_file(self, file_path):
71-
return file_path if not self.file_prefix else self.file_prefix+file_path
72-
7345
def _create_json(self, file_info):
7446
return {
7547
"timestamp": datetime.now().isoformat(),
76-
"paths": [self._prefix_file(fp) for fp in file_info['paths']],
48+
"paths": file_info['paths'],
7749
'adler32': file_info['adler32'],
7850
'file_size': file_info['file_size'],
7951
'file_events': file_info['file_events']
@@ -86,7 +58,7 @@ def put_file_add(self, file_info):
8658
try:
8759
mesg = self._create_json(file_info)
8860
requests.put(f"{self.endpoint}{self.dataset_id}/files", json=mesg)
89-
self.logger.info(f"Metric: {json.dumps(mesg)}")
61+
self.logger.info("adding file:", extra=file_info)
9062
success = True
9163
except requests.exceptions.ConnectionError:
9264
self.logger.exception(f'Connection error to ServiceX App. Will retry '

tests/servicex_did_finder_lib/test_communication.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import argparse
22
import json
33
from typing import Any, AsyncGenerator, Dict, Optional
4-
from unittest.mock import ANY, MagicMock, patch
4+
from unittest.mock import MagicMock, patch
55

66
import pika
77
import pytest
@@ -27,7 +27,6 @@ def send_did_request(self, did_name: str):
2727
properties = MagicMock()
2828
body = json.dumps(
2929
{
30-
"request_id": '123-456',
3130
"did": did_name,
3231
"dataset_id": "000-111-222-444",
3332
"endpoint": "http://localhost:2334/",
@@ -315,7 +314,6 @@ async def my_callback(
315314
# Make sure the file was sent along, along with the completion
316315
SXAdaptor.put_file_add.assert_not_called()
317316
SXAdaptor.put_fileset_complete.assert_not_called()
318-
SXAdaptor.post_status_update.assert_any_call(ANY, severity="fatal")
319317

320318

321319
def test_failed_file_after_good(rabbitmq, SXAdaptor):
@@ -344,7 +342,6 @@ async def my_callback(
344342
# Make sure the file was sent along, along with the completion
345343
SXAdaptor.put_file_add.assert_called_once()
346344
SXAdaptor.put_fileset_complete.assert_not_called()
347-
SXAdaptor.post_status_update.assert_any_call(ANY, severity="fatal")
348345

349346

350347
def test_failed_file_after_good_with_avail(rabbitmq, SXAdaptor):
@@ -373,7 +370,6 @@ async def my_callback(
373370
# Make sure the file was sent along, along with the completion
374371
SXAdaptor.put_file_add.assert_called_once()
375372
SXAdaptor.put_fileset_complete.assert_called_once()
376-
SXAdaptor.post_status_update.assert_any_call("Completed load of files in 0 seconds")
377373

378374

379375
def test_failed_file_after_good_with_avail_limited_number(rabbitmq, SXAdaptor):
@@ -408,7 +404,6 @@ async def my_callback(
408404
# Make sure the file was sent along, along with the completion
409405
SXAdaptor.put_file_add_bulk.assert_called_once()
410406
SXAdaptor.put_fileset_complete.assert_called_once()
411-
SXAdaptor.post_status_update.assert_any_call("Completed load of files in 0 seconds")
412407

413408

414409
def test_no_files_returned(rabbitmq, SXAdaptor):
@@ -433,7 +428,6 @@ async def my_callback(
433428
SXAdaptor.put_file_add.assert_not_called()
434429
SXAdaptor.put_fileset_complete.assert_called_once()
435430
assert SXAdaptor.put_fileset_complete.call_args[0][0]["files"] == 0
436-
SXAdaptor.post_status_update.assert_any_call(ANY, severity="fatal")
437431

438432

439433
def test_rabbitmq_connection_failure(rabbitmq_fail_once, SXAdaptor):
@@ -532,8 +526,6 @@ async def my_user_callback(did, info):
532526
assert SXAdaptor.put_fileset_complete.call_args[0][0]["total-events"] == 192
533527
assert SXAdaptor.put_fileset_complete.call_args[0][0]["total-bytes"] == 3070
534528

535-
assert SXAdaptor.post_status_update.called_once()
536-
537529

538530
@pytest.mark.asyncio
539531
async def test_run_file_bulk_fetch_loop(SXAdaptor, mocker):
@@ -570,8 +562,6 @@ async def my_user_callback(did, info):
570562
assert SXAdaptor.put_fileset_complete.call_args[0][0]["total-events"] == 192
571563
assert SXAdaptor.put_fileset_complete.call_args[0][0]["total-bytes"] == 3070
572564

573-
assert SXAdaptor.post_status_update.called_once()
574-
575565

576566
@pytest.mark.asyncio
577567
async def test_run_file_fetch_one(SXAdaptor, mocker):
@@ -609,7 +599,6 @@ async def my_user_callback(did, info):
609599

610600
SXAdaptor.put_fileset_complete.assert_called_once
611601
assert SXAdaptor.put_fileset_complete.call_args[0][0]["files"] == 1
612-
assert SXAdaptor.post_status_update.called_once()
613602

614603

615604
@pytest.mark.asyncio
@@ -650,7 +639,6 @@ async def my_user_callback(did, info):
650639

651640
SXAdaptor.put_fileset_complete.assert_called_once
652641
assert SXAdaptor.put_fileset_complete.call_args[0][0]["files"] == 1
653-
assert SXAdaptor.post_status_update.called_once()
654642

655643

656644
@pytest.mark.asyncio
@@ -689,7 +677,6 @@ async def my_user_callback(did, info):
689677

690678
SXAdaptor.put_fileset_complete.assert_called_once
691679
assert SXAdaptor.put_fileset_complete.call_args[0][0]["files"] == 1
692-
assert SXAdaptor.post_status_update.called_once()
693680

694681

695682
@pytest.mark.asyncio
@@ -703,10 +690,3 @@ async def my_user_callback(did, info):
703690

704691
assert SXAdaptor.put_file_add.assert_not_called
705692
SXAdaptor.put_fileset_complete.assert_called_once()
706-
707-
assert (
708-
SXAdaptor.post_status_update.call_args_list[0][0][0]
709-
== "DID Finder found zero files for dataset 123-456"
710-
)
711-
712-
assert SXAdaptor.post_status_update.call_args_list[0][1]["severity"] == "fatal"

tests/servicex_did_finder_lib/test_servicex_did.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,3 @@ def test_put_file_add_bulk_large():
6868
'file_events': 3141
6969
}] * 32)
7070
assert len(responses.calls) == 2
71-
72-
73-
@responses.activate
74-
def test_put_file_add_with_prefix():
75-
76-
responses.add(responses.PUT, 'http://servicex.org/12345/files', status=206)
77-
sx = ServiceXAdapter("http://servicex.org/", '12345', file_prefix="xcache123:")
78-
sx.put_file_add({
79-
'paths': ['root://foo.bar.ROOT'],
80-
'adler32': '32',
81-
'file_size': 1024,
82-
'file_events': 3141
83-
})
84-
85-
assert len(responses.calls) == 1
86-
submitted = json.loads(responses.calls[0].request.body)
87-
assert submitted['paths'][0] == 'xcache123:root://foo.bar.ROOT'
88-
assert submitted['adler32'] == '32'
89-
assert submitted['file_events'] == 3141
90-
assert submitted['file_size'] == 1024

0 commit comments

Comments
 (0)