MediaMath / t1-python

Python SDK for MediaMath Platform APIs
https://developer.mediamath.com/
Apache License 2.0
18 stars 30 forks source link

T1Error: 'Unknown Error. Code: conflict. Message: Version Conflict' #148

Closed wolfhund closed 7 years ago

wolfhund commented 7 years ago

Hello. We have a system that uses the terminalone python library with these characteristics: TerminalOne==1.6.1 Python=2.7 Linux with Debian==8.6

We do a simple process: Sending a request to change a campaign status and save its report to our DB. This is how we do it in our code:

# to update campaign status
t1 = terminalone.T1(username, password, api_key)
campaign = t1.get("campaigns", campaign_id)
campaign.status = new_status
campaign.save()
# to get campaign reports
t1 = terminalone.T1(username, password, api_key)
rpts = t1.new("report")
report = t1.new("report", rpts.report_uri("performance"))
report.set({
    "filter": {"campaign_id": campaign_id},
    "metrics": ["impressions", "clicks", "ctr", "total_ad_cost_cpc", "total_ad_cost_cpm"],
    "time_window": "yesterday",
    "time_rollup": "by_week",
    "dimensions": ["campaign_id"],
})
headers, data = report.get()

Everything was working fine, but then we got this error log from last weekend:

File "/usr/local/lib/python2.7/dist-pakages/terminalone/connection.py", line 202, in _post
  return self._parse_response(response)
File "/usr/local/lib/python2.7/dist-packages/terminalone/connection.py", line 216, in _parse_response
  result = parser(response_body)
File "/usr/local/lib/python2.7/dist-packages/terminalone/xmlparser.py", line 27, in __init__
  self.get_status(result, xml)
File "/usr/local/lib/python2.7/dist-packages/terminalone/xmlparser.py", line 66, in get_status
  raise T1Error(status_code, message)
T1Error: 'Unknown Error. Code: conflict. Message: Version Conflict'

We could not reproduce this error again yet but we fear that we might have it again. By analyzing your source code in the xmlparser.py file, I saw that you are expecting to receive an existent key as the status, otherwise a KeyError is thrown. We always send True or False as the new status for the campaign, so we were thinking that might be related to the way you expect to parse this keys? Maybe something that wasn't improve in the 1.6 versions. Should we upgrade to 1.7 already?

Thanks in advance.

FodT commented 7 years ago

Hi @wolfhund,

the 'version conflict' response is returned when the version field of an entity POST request differs from that currently in T1. It could have been that the entity was updated in parallel by another process (perhaps a logged in user in t1, or another script).

I'm unsure why you think this problem is due to xmlparser: There's no KeyError being thrown here.

wolfhund commented 7 years ago

Thanks for your fast reply. We actually use python Threads to handle many requests at time. Do you think this could cause that two Threads called the same campaign id to update its status at the same time? Would this raise the 'version conflict' exception?

FodT commented 7 years ago

Yep that could well be the case if you have multiple threads each attempting to update the same entity.

May I ask if there is a specific workflow here? I'm curious to know if there are high frequency updates on entities.

wolfhund commented 7 years ago

Thanks for your feedback. We use it for tvsync, so we trigger this event every time our system detects a commercial from many different channels. It is not that frequently, but we could get this kind of error in the future as we keep increasing our commercial detections.

We will handle this problem from our side. I am setting this issue as closed.

Regards.