markhoerth opened a new issue, #14753:
URL: https://github.com/apache/gravitino/issues/14753

   ### Version
   
   main branch, commit 426f8908ef69175e54f14aea04259381436c66c0 (2.0.0-SNAPSHOT)
   
   ### Describe what's wrong
   
   When the owner of one metadata object is changed several times in quick 
succession (for example two clients, or two Gravitino servers sharing one 
PostgreSQL entity store, alternating `PUT /owners/{type}/{name}` between two 
users), some of the owner changes are rejected with HTTP 500 and a PostgreSQL 
duplicate key error on `owner_meta`. The user expects every owner change to 
succeed (or to get a 409 if it conflicts). The failed change is rolled back, so 
the owner stays as it was, but the caller gets a 500 that tells them nothing 
useful.
   
   It shows up on a single server as well as on two servers. In local runs, 8 
parallel clients alternating 200 owner changes on one model got 3 failures 
against one server and 9 against two servers. The entity store was PostgreSQL 
in both cases.
   
   Cause:
   
   - The owner change retires the current owner row with a soft delete that 
sets `deleted_at` to the database time in milliseconds: 
`core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/postgresql/OwnerMetaPostgreSQLProvider.java:44-53`
 (`softDeleteOwnerRelByMetadataObjectIdAndType`), called from 
`core/src/main/java/org/apache/gravitino/storage/relational/service/OwnerMetaService.java:199-204`
 inside `setOwner` (line 175) / `assignOwner` (line 283).
   - On PostgreSQL that time is `CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 
1000 AS BIGINT)` 
(`core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/DatabaseTimeSQL.java:43-44`).
 `CURRENT_TIMESTAMP` in PostgreSQL is the start time of the transaction, not 
the time of the statement.
   - `owner_meta` has `UNIQUE (owner_id, metadata_object_id, 
metadata_object_type, deleted_at)` 
(`scripts/postgresql/schema-2.0.0-postgresql.sql:570`).
   - Owner writes on one object are serialized by the row locks taken in 
`assignOwner` (`OwnerMetaService.java:283-290`), so concurrent requests queue 
there. Their transactions can start within the same millisecond, though. Say 
user P owns the object and three such transactions, all started in millisecond 
T, run one after another: set Q retires P's row with `deleted_at = T`, set P 
retires Q's row with `deleted_at = T` and inserts a live row for P, then set Q 
tries to retire P's new row with `deleted_at = T`. That row would duplicate the 
first retired row for P, so the UPDATE fails with a unique-constraint 
violation, which surfaces as a 500. Setting the same owner twice within one 
millisecond hits it the same way.
   
   The retired-row key needs a value that cannot repeat for the same owner and 
object (for example the row id, a sequence, or `clock_timestamp()` combined 
with a retry), or the unique key should be defined only over live rows 
(`deleted_at = 0`).
   
   ### Error message and/or stacktrace
   
   ```
   HTTP 500
   {"code":1002,"type":"RuntimeException","message":"Failed to operate object 
[metadata object mc.s.m] operation [SET] under [repro_own_ml], reason [ERROR: 
duplicate key value violates unique constraint 
\"owner_meta_owner_id_metadata_object_id_metadata_object_type_key\"\n  Detail: 
Key (owner_id, metadata_object_id, metadata_object_type, 
deleted_at)=(2812392764185999920, 2279714622612267605, MODEL, 1794182197777) 
already exists.]", ...}
   
   ### The error may involve 
org.apache.gravitino.storage.relational.mapper.OwnerMetaMapper.softDeleteOwnerRelByMetadataObjectIdAndType-Inline
     at 
org.apache.gravitino.storage.relational.service.OwnerMetaService.lambda$setOwner$7(OwnerMetaService.java:203)
     at 
org.apache.gravitino.storage.relational.utils.SessionUtils.doWithoutCommit(SessionUtils.java:86)
     at 
org.apache.gravitino.storage.relational.service.OwnerMetaService.lambda$setOwner$8(OwnerMetaService.java:200)
     at 
org.apache.gravitino.storage.relational.utils.SessionUtils.doMultipleWithCommit(SessionUtils.java:102)
     at 
org.apache.gravitino.storage.relational.service.OwnerMetaService.assignOwner(OwnerMetaService.java:283)
     at 
org.apache.gravitino.storage.relational.service.OwnerMetaService.setOwner(OwnerMetaService.java:191)
     at 
org.apache.gravitino.storage.relational.JDBCBackend.insertRelation(JDBCBackend.java:764)
     at 
org.apache.gravitino.storage.relational.RelationalEntityStore.insertRelation(RelationalEntityStore.java:461)
     at 
org.apache.gravitino.authorization.OwnerManager.setOwner(OwnerManager.java:79)
   ```
   
   The nonzero `deleted_at` in the key shows the soft-delete UPDATE is the 
failing statement, not the INSERT of the new owner row.
   
   ### How to reproduce
   
   Gravitino server with a PostgreSQL entity store and 
`gravitino.authorization.enable = true`, simple auth, at 
`http://127.0.0.1:8090`.
   
   1. Set up a metalake, a model catalog, a schema, a model and two users:
   ```
   g() { curl -s -H "Authorization: Basic YWRtaW46eA==" -H "Content-Type: 
application/json" -H "Accept: application/vnd.gravitino.v1+json" "$@"; echo; }
   S=http://127.0.0.1:8090/api/metalakes
   g -X POST $S -d '{"name":"repro_own_ml","comment":"","properties":{}}'
   g -X POST $S/repro_own_ml/catalogs -d 
'{"name":"mc","type":"MODEL","provider":"model","comment":"","properties":{}}'
   g -X POST $S/repro_own_ml/catalogs/mc/schemas -d 
'{"name":"s","comment":"","properties":{}}'
   g -X POST $S/repro_own_ml/users -d '{"name":"p"}'
   g -X POST $S/repro_own_ml/users -d '{"name":"q"}'
   g -X POST $S/repro_own_ml/catalogs/mc/schemas/s/models -d 
'{"name":"m","comment":"","properties":{}}'
   ```
   2. Alternate the owner between p and q with 8 parallel clients, 200 requests:
   ```
   for k in $(seq 1 200); do [ $((k/2%2)) = 0 ] && echo p || echo q; done | 
xargs -P 8 -I{} \
     curl -s -o /dev/null -w "%{http_code}\n" -X PUT \
     -H "Authorization: Basic YWRtaW46eA==" -H "Content-Type: application/json" 
-H "Accept: application/vnd.gravitino.v1+json" \
     http://127.0.0.1:8090/api/metalakes/repro_own_ml/owners/model/mc.s.m -d 
'{"name":"{}","type":"USER"}' | sort | uniq -c
   ```
   3. Some of the responses are 500 with the body above. The count varies 
between runs: 3 and 8 of 200 against one server, 9 of 200 when the requests 
were spread over two servers on the same store.
   
   ### Additional context
   
   - The defect is in Gravitino's own entity-store code (the soft-delete 
timestamp and the `owner_meta` unique key); no upstream library is involved.
   - Related: #8293 (closed), a flaky unit test that hit the same millisecond 
`deleted_at` collision on H2 through `softDeleteOwnerRelByOwnerIdAndType`. 
Other soft deletes that write the millisecond time into a unique key may be 
exposed the same way; only the owner path was reproduced here.
   - Also present in v1.3.1: `OwnerMetaService.setOwner` there runs the same 
soft delete with the same PostgreSQL expression (`CAST(EXTRACT(EPOCH FROM 
CURRENT_TIMESTAMP) * 1000 AS BIGINT)`), and 
`scripts/postgresql/schema-1.3.0-postgresql.sql` has the same unique key on 
`owner_meta`.
   - Found by the Gravitino 2.0 OSS test suite check 
`test_delete_after_fast_ownership_churn_never_500` (ownership of a model 
flipped between two users from two servers, then the model deleted; 2 of 630 
responses were this 500).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to