Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeadlLocks may happen at LASTDATA POSTGRES SINK when upserting batches in HA scenarios #2197

Closed
mrutid opened this issue Sep 28, 2022 · 1 comment

Comments

@mrutid
Copy link
Member

mrutid commented Sep 28, 2022

Some deadlocks are arising at Postgres LastData Sink.

DEADLOCK
AGGREGATION-A UPSERT (-id:1,-id:...,-id:N)
AGGREGATION-B UPSERT (-id:N,-id:...,-id:1)

Sort aggregations by entity_id (at least) may help to avoid the problem.

NO DEADLOCK
AGGREGATION-A UPSERT (-id:1,-id:...,-id:N)
AGGREGATION-B UPSERT (-id:1,-id:...,-id:N)

Seems that each aggregation has the form:

{
   entity_id:  [id1,...idn]
   attr1:         [v1,..vn],
...
   attrM:       [v1,..vn]
}

There are multiple INSERT queries followed by a single Commit. We should order each query (by any common criteria).

https://github.com/telefonicaid/fiware-cygnus/blob/master/cygnus-common/src/main/java/com/telefonica/iot/cygnus/backends/sql/SQLBackendImpl.java#L610

This kind of snippet should make the work:

import java.util.ArrayList;
import java.util.Comparator;
upsertList.sort(Comparator.comparing(buff -> buff.toString()));

https://www.sololearn.com/compiler-playground/ceuF34hEdPlD

We should also change the attribute order within the query, so Time values, which is no key occurs later:

INSERT INTO watercycle_tank_lastdata (recvTime,fiwareServicePath,entityId,entityType,...)

to

INSERT INTO watercycle_tank_lastdata (entityId,entityType,fiwareServicePath recvTime,...)

aggregation.put(NGSIConstants.RECV_TIME_TS+"C", new ArrayList<JsonElement>());

@fgalan
Copy link
Member

fgalan commented Oct 7, 2022

PR #2199

@fgalan fgalan added this to the release/2.20.0 milestone Oct 13, 2022
@fgalan fgalan closed this as completed Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants