SSHOC / sshoc-marketplace-backend

Code for the backend
Apache License 2.0
2 stars 0 forks source link

Error when publishing a draft with externalIds #166

Closed dpancic closed 1 year ago

dpancic commented 2 years ago

In GitLab by @KlausIllmayer on Apr 6, 2022, 14:21

Every now and then there was an error in the logfiles that also had an effect on publishing an item. The error lead to a 500 error on frontend and in the logfiles it said:

ERROR e.s.m.c.MarketplaceExceptionHandler.handleServerError - Runtime exception
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [item_external_ids_identifier_service_code_identifier_item_i_key]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:298)

I'm now able to repeat the error on our stage instance (all using the frontend):

In the logfile you will see:

06-04-2022 12:11:58.107 [http-nio-8080-exec-6] WARN  o.h.e.jdbc.spi.SqlExceptionHelper.logExceptions - SQL Error: 0, SQLState: 23505
06-04-2022 12:11:58.107 [http-nio-8080-exec-6] ERROR o.h.e.jdbc.spi.SqlExceptionHelper.logExceptions - ERROR: duplicate key value violates unique constraint "item_external_ids_identifier_service_code_identifier_item_i_key"
  Detail: Key (identifier_service_code, identifier, item_id)=(GitHub, test/test, 40766) already exists.
06-04-2022 12:11:58.110 [http-nio-8080-exec-6] ERROR e.s.m.c.MarketplaceExceptionHandler.handleServerError - Runtime exception
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [item_external_ids_identifier_service_code_identifier_item_i_key]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:298)

As the same workflow works like a charm if you don't save a draft but immediately publish the item, I suspect that publishing the draft item is done in a way so that it breaks the mentioned constraint.

@tparkola Could you look into this issue? It happend until now not that often, but as it is easy to initiate we need a bugfix for this.

Informing @laureD19 (already having this issue last week - thanks for reporting, it helped me to reconstruct the problem) and @vronk

dpancic commented 2 years ago

In GitLab by @stefanprobst on Apr 6, 2022, 16:32

one way to reproduce:

(1) create new draft item

curl "http://localhost:8080/api/datasets?draft=true" -X POST -H "content-type: application/json" -H "authorization: ${TOKEN}" -d '{"label":"l","description":"d","externalIds":[{"identifier":"123","identifierService":{"code":"GitHub"}}]}'

(2) update draft (persistent id taken from response above)

curl "http://localhost:8080/api/datasets/9faRzb?draft=true" -X PUT -H "content-type: application/json" -H "authorization: ${TOKEN}" -d '{"label":"l","description":"d","externalIds":[{"identifier":"123","identifierService":{"code":"GitHub"}}]}'
dpancic commented 2 years ago

In GitLab by @tparkola on Apr 22, 2022, 12:03

Currently I just finished fixing error connected to externalIds, but noticed inaccuracy. So I did as wroted in scenario, but then I encountered error - draft item not found. I was confused - Unable to find draft eu.sshopencloud.marketplace.model.tools.Tool with id 8P5hKY for the authorized user It happends after clicking button publish. The backed is called 3 times for item 8P5hKY - twice for not draft, and once for draft whereas the draft item was published therefore deleted from table drafts. Don't know if the draft item should't be deleted, or front should not call for draft for this item right away? @KlausIllmayer @stefanprobst @vronk

dpancic commented 2 years ago

In GitLab by @stefanprobst on Apr 22, 2022, 14:32

The backed is called 3 times for item 8P5hKY

these calls happen on the item details page (after the user has been redirected there after clicking "Publish"), correct? if so, the reason is that on an item details page, we show potentially three different "Edit" buttons to authenticated users: "Edit the currently approved version", "Edit the latest suggested version", "Edit the latest draft version". If there is a more straightforward way to know whether there are currently suggested or draft item versions saved by a user, i'd be happy to know.

dpancic commented 2 years ago

In GitLab by @tparkola on Apr 25, 2022, 08:59

Now I understand, thanks. Unfortunately, I don't think there is.

dpancic commented 2 years ago

In GitLab by @KlausIllmayer on Jun 28, 2022, 15:54

I've tried out both approaches (mine and Stefan's) on stage instance (Stefan's also on production) and the error does not occur anymore. Thanks for solving!

dpancic commented 2 years ago

In GitLab by @KlausIllmayer on Jun 28, 2022, 15:54

changed the incident status to Resolved by closing the incident