@@ -13,6 +13,7 @@ import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messag
1313import { getExperimentsClientForBackend } from "@gitpod/gitpod-protocol/lib/experiments/configcat-server" ;
1414import { v1 } from "@authzed/authzed-node" ;
1515import { fgaRelationsUpdateClientLatency } from "../prometheus-metrics" ;
16+ import { RedisMutex } from "../redis/mutex" ;
1617
1718@injectable ( )
1819export class RelationshipUpdater {
@@ -23,6 +24,7 @@ export class RelationshipUpdater {
2324 @inject ( TeamDB ) private readonly orgDB : TeamDB ,
2425 @inject ( ProjectDB ) private readonly projectDB : ProjectDB ,
2526 @inject ( Authorizer ) private readonly authorizer : Authorizer ,
27+ @inject ( RedisMutex ) private readonly mutex : RedisMutex ,
2628 ) { }
2729
2830 /**
@@ -42,6 +44,12 @@ export class RelationshipUpdater {
4244 } ,
4345 } ) ;
4446 if ( ! fgaEnabled ) {
47+ if ( user . additionalData ?. fgaRelationshipsVersion !== undefined ) {
48+ log . info ( { userId : user . id } , `User has been removed from FGA.` ) ;
49+ // reset the fgaRelationshipsVersion to undefined, so the migration is triggered again when the feature is enabled
50+ AdditionalUserData . set ( user , { fgaRelationshipsVersion : undefined } ) ;
51+ return await this . userDB . storeUser ( user ) ;
52+ }
4553 return user ;
4654 }
4755 if ( user . additionalData ?. fgaRelationshipsVersion === this . version ) {
@@ -53,41 +61,40 @@ export class RelationshipUpdater {
5361 fromVersion : user ?. additionalData ?. fgaRelationshipsVersion ,
5462 toVersion : this . version ,
5563 } ) ;
56- // TODO run in distributed lock
57- // return this.mutex.using([`fga-migration-${user.id}`], 2000, async () => {
58- const before = new Date ( ) . getTime ( ) ;
64+ return this . mutex . using ( [ `fga-migration-${ user . id } ` ] , 2000 , async ( ) => {
65+ const before = new Date ( ) . getTime ( ) ;
5966
60- const updatedUser = await this . userDB . findUserById ( user . id ) ;
61- if ( ! updatedUser ) {
62- throw new ApplicationError ( ErrorCodes . NOT_FOUND , "User not found" ) ;
63- }
64- user = updatedUser ;
65- if ( user . additionalData ?. fgaRelationshipsVersion === this . version ) {
66- return user ;
67- }
68- log . info ( { userId : user . id } , `Updating FGA relationships for user.` , {
69- fromVersion : user ?. additionalData ?. fgaRelationshipsVersion ,
70- toVersion : this . version ,
71- } ) ;
72- const orgs = await this . findAffectedOrganizations ( user . id ) ;
67+ const updatedUser = await this . userDB . findUserById ( user . id ) ;
68+ if ( ! updatedUser ) {
69+ throw new ApplicationError ( ErrorCodes . NOT_FOUND , "User not found" ) ;
70+ }
71+ user = updatedUser ;
72+ if ( user . additionalData ?. fgaRelationshipsVersion === this . version ) {
73+ return user ;
74+ }
75+ log . info ( { userId : user . id } , `Updating FGA relationships for user.` , {
76+ fromVersion : user ?. additionalData ?. fgaRelationshipsVersion ,
77+ toVersion : this . version ,
78+ } ) ;
79+ const orgs = await this . findAffectedOrganizations ( user . id ) ;
7380
74- // Add relationships
75- await this . updateUser ( user ) ;
76- for ( const org of orgs ) {
77- await this . updateOrganization ( user . id , org ) ;
78- }
79- AdditionalUserData . set ( user , {
80- fgaRelationshipsVersion : this . version ,
81- } ) ;
82- await this . userDB . updateUserPartial ( {
83- id : user . id ,
84- additionalData : user . additionalData ,
85- } ) ;
86- log . info ( { userId : user . id } , `Finished updating relationships.` , {
87- duration : new Date ( ) . getTime ( ) - before ,
81+ // Add relationships
82+ await this . updateUser ( user ) ;
83+ for ( const org of orgs ) {
84+ await this . updateOrganization ( user . id , org ) ;
85+ }
86+ AdditionalUserData . set ( user , {
87+ fgaRelationshipsVersion : this . version ,
88+ } ) ;
89+ await this . userDB . updateUserPartial ( {
90+ id : user . id ,
91+ additionalData : user . additionalData ,
92+ } ) ;
93+ log . info ( { userId : user . id } , `Finished updating relationships.` , {
94+ duration : new Date ( ) . getTime ( ) - before ,
95+ } ) ;
96+ return user ;
8897 } ) ;
89- return user ;
90- // });
9198 } catch ( error ) {
9299 log . error ( { userId : user . id } , `Error updating relationships.` , error ) ;
93100 return user ;
0 commit comments