Skip to content

Commit 2eba398

Browse files
committed
Improving composite annotations uploads process
1 parent a8a97f1 commit 2eba398

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

HISTORY.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
History
44
-------
55

6+
9.8.2 (2025-03-21)
7+
------------------
8+
9+
- Retrying annotations update to avoid temporary concurrency issues in
10+
source composites updates.
11+
612
9.8.1 (2025-01-14)
713
------------------
814

bigml/api_handlers/sourcehandler.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import sys
2525
import os
2626
import numbers
27+
import time
28+
import logging
2729

2830
from urllib import parse
2931

@@ -67,8 +69,13 @@
6769
from bigml.api_handlers.resourcehandler import ResourceHandlerMixin, LOGGER
6870
from bigml.fields import Fields
6971

72+
LOG_FORMAT = '%(asctime)-15s: %(message)s'
73+
LOGGER = logging.getLogger('BigML')
74+
CONSOLE = logging.StreamHandler()
75+
CONSOLE.setLevel(logging.WARNING)
76+
LOGGER.addHandler(CONSOLE)
7077

71-
MAX_CHANGES = 500
78+
MAX_CHANGES = 5
7279

7380

7481
def compact_regions(regions):
@@ -508,6 +515,8 @@ def update_composite_annotations(self, source, images_file,
508515
try:
509516
_ = file_list.index(filename)
510517
except ValueError:
518+
LOGGER.error("WARNING: Could not find annotated file (%s)"
519+
" in the composite's sources list", filename)
511520
continue
512521
for key in annotation.keys():
513522
if key == "file":
@@ -550,16 +559,33 @@ def update_composite_annotations(self, source, images_file,
550559
"value": value,
551560
"components": [source_id]})
552561
except Exception:
562+
LOGGER.error("WARNING: Problem adding annotation to %s (%s)",
563+
field, values)
553564
pass
554565

555566
# we need to limit the amount of changes per update
556567
batches_number = int(len(changes) / MAX_CHANGES)
557568
for offset in range(0, batches_number + 1):
558-
new_batch = changes[offset * MAX_CHANGES: (offset + 1) * MAX_CHANGES]
569+
new_batch = changes[
570+
offset * MAX_CHANGES: (offset + 1) * MAX_CHANGES]
559571
if new_batch:
560572
source = self.update_source(source,
561573
{"row_values": new_batch})
562-
self.ok(source)
574+
if source["error"] is not None:
575+
# retrying in case update is temporarily unavailable
576+
time.sleep(1)
577+
source = self.get_source(source)
578+
self.ok(source)
579+
source = self.update_source(source,
580+
{"row_values": new_batch})
581+
if source["error"] is not None:
582+
LOGGER.error("WARNING: Some annotations were not"
583+
" updated (%s)",
584+
new_batch)
585+
if not self.ok(source):
586+
raise Exception(
587+
f"Failed to update {len(new_batch)} annotations.")
588+
time.sleep(0.1)
563589

564590
return source
565591

bigml/bigmlconnection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ def _create(self, url, body, verify=None, organization=None):
406406
error = json_load(response.content)
407407
LOGGER.error(self.error_message(error, method='create'))
408408
elif code != HTTP_ACCEPTED:
409-
LOGGER.error("Unexpected error (%s)", code)
409+
LOGGER.error("CREATE Unexpected error (%s)", code)
410410
code = HTTP_INTERNAL_SERVER_ERROR
411411
except ValueError as exc:
412412
LOGGER.error("Malformed response: %s", str(exc))
@@ -489,7 +489,7 @@ def _get(self, url, query_string='',
489489
LOGGER.error(self.error_message(error, method='get',
490490
resource_id=resource_id))
491491
else:
492-
LOGGER.error("Unexpected error (%s)", code)
492+
LOGGER.error("GET Unexpected error (%s)", code)
493493
code = HTTP_INTERNAL_SERVER_ERROR
494494

495495
except ValueError as exc:
@@ -582,7 +582,7 @@ def _list(self, url, query_string='', organization=None):
582582
HTTP_TOO_MANY_REQUESTS]:
583583
error = json_load(response.content)
584584
else:
585-
LOGGER.error("Unexpected error (%s)", code)
585+
LOGGER.error("LIST Unexpected error (%s)", code)
586586
code = HTTP_INTERNAL_SERVER_ERROR
587587
except ValueError as exc:
588588
LOGGER.error("Malformed response: %s", str(exc))
@@ -662,7 +662,7 @@ def _update(self, url, body, organization=None, resource_id=None):
662662
LOGGER.error(self.error_message(error, method='update',
663663
resource_id=resource_id))
664664
else:
665-
LOGGER.error("Unexpected error (%s)", code)
665+
LOGGER.error("UPDATE Unexpected error (%s)", code)
666666
code = HTTP_INTERNAL_SERVER_ERROR
667667
except ValueError:
668668
LOGGER.error("Malformed response")

bigml/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '9.8.1'
1+
__version__ = '9.8.2'

0 commit comments

Comments
 (0)