Repository navigation
fix: Introducing initializers for handling lock cleanup on start - #1342
Conversation
jvherweg
left a comment
There was a problem hiding this comment.
Mostly minor comments, looks good. RELEASE_NOTES still needs to be update to indicate the config/interface changes.
Any idea why the integration tests are failing though? Seems as there is still a potential race condition with locks being removed by 2 different handlers?
| "@id": "urn:solid-server:default:PrimarySequenceInitializer", | ||
| "@type":"SequenceHandler", | ||
| "handlers": [ | ||
| { "@id": "urn:solid-server:default:StartCleanupInitializer"}, |
There was a problem hiding this comment.
Would go for CleanupInitializer and CleanupFinalizer as the start/stop is already implied by the name.
| ] | ||
| }, | ||
| { | ||
| "comment": "These finalizers will all be executed sequentially as last when stopping the server.", |
There was a problem hiding this comment.
| "comment": "These finalizers will all be executed sequentially as last when stopping the server.", | |
| "comment": "These finalizers will all be executed sequentially last when stopping the server.", |
| { "@id": "urn:solid-server:default:Finalizer" }, | ||
| { "@id": "urn:solid-server:default:StopCleanupFinalizer" } |
There was a problem hiding this comment.
Do we currently have any issues if these are not split up or is this just to mirror the initializer approach?
There was a problem hiding this comment.
I am not aware of any concrete issues (as opposed to what I experienced with the Initializers). The rationale was that cleanup (such as deleting a lock folder) could negatively impact other finalizers that e.g. operate on the ResourceStore. So you could say I mirrored the initializer approach.
There was a problem hiding this comment.
Fair enough. Felt a bit too much since we don't really need it right now, but might not be bad to already add it.
| "expiration": 3000 | ||
| }, | ||
| { | ||
| "comment": "Makes sure the FileSystemResourceLocker starts with a clean slate when the application is started.", |
There was a problem hiding this comment.
This comment should actually be on the InitializableHandler below since this is RDF and otherwise all comments will be attached to the StartCleanupInitializer. Doesn't matter that much in practice since I assume everyone just reads the json-ld files directly, but can be correct here since the effect is the same.
For example below we did this wrong in the finalizer but then we didn't have a specific class yet to add that comment though so we cheated by adding it to the main finalizer. But that can also be changed now.
Would also rephrase the comment to focus on the why (to prevent dangling locks that could cause a conflict).
| "expiration": 3000 | ||
| }, | ||
| { | ||
| "comment": "Makes sure the RedisLocker starts with a clean slate when the application is started.", |
| } | ||
|
|
||
| private async clearLocks(): Promise<void> { | ||
| // Remove any lock still open |
There was a problem hiding this comment.
Move this to tsdoc above the function.
|
|
||
| beforeAll(async(): Promise<void> => { | ||
| // Clean the working dir | ||
| await removeFolder(rootFilePath); |
There was a problem hiding this comment.
Why is this necessary? If this folder is not empty it means something has gone wrong with the test run (since afterAll also removes the folder), which would also be an issue for many other tests that write files to the disk.
There was a problem hiding this comment.
Good point! I sometimes terminate the process when debugging, which can lead to folder content remaining on disk. I will remove this statement, as this will never occur in the CI pipeline.
| ) as Record<string, any>; | ||
| ({ app, store, locker } = instances); | ||
|
|
||
| // Call locker.initialize() manually, so we can create a test resource |
There was a problem hiding this comment.
Why is this necessary to create a test resource? The config uses the backend resource store so it doesn't pass through the locking store.
There was a problem hiding this comment.
Hmm, I had an error when I didn't initialize before creating the test resource.. I will investigate!
There was a problem hiding this comment.
So apparently, it's a call to ensureDir of the lockfolder that is required for the test resource creation to succeed.
This used to be called from the FileSystemResourceLocker constructor, but I've moved this to the initialize() implementation. I guess the resource store expects the root folder to exist, which is not guaranteed at this point (as the test has not started the server), and ensureDir for the lock-folder, also creates the parent folders, thus avoiding this issue.
There was a problem hiding this comment.
The resource store should be able to handle the fact that the root container does not exist yet and create it in the process. We used to have a memory locking system and then everything worked fine then so I'm not exactly sure what the problem is. Not starting the server yet does mean that the root initializer has not created the root container yet, but that one also just does it by calling the resource store, so why would that call succeed and this one fail?
|
|
||
| it('should not be affected by dangling locks.', async(): Promise<void> => { | ||
| // Simulate lock existing before server startup, by creating a (write) lock directly | ||
| const promise = locker.withWriteLock(resourceIdentifier, async(): Promise<void> => { |
There was a problem hiding this comment.
Why is this test (slightly) different from the file one? If they can be the same this can be generalized.
| @@ -0,0 +1,63 @@ | |||
| { | |||
There was a problem hiding this comment.
Can this config (and the other new one below) potentially be merged with one of the already existing ones? By introducing a new entry in the recordobject or defining the resource store outside of the config like we do in some other integration tests for example. That way we have less configs we have to keep up to date.
|
This also clears the locks of the read-write locker since it removes the same folder right? Should probably mention that in a comment somewhere and create a new issue afterwards that we should look into doing this in a cleaner way so there is no codependency. |
jvherweg
left a comment
There was a problem hiding this comment.
Mostly documentation comments
| - The init handlers were restructured with the goal of introducing a new initialization phase that is executed right after initialization of the logging infrastructure, but before everything else. This phase is represented by a `SequenceHandler` with id `urn:solid-server:default:CleanupInitializer`. Critical init operations (on which subsequent init handlers also depend), especially related to cleaning up the server state before launch, must be added here. This extra phase was mirrored for finalization (`SequenceHandler` with id `CleanupFinalizer`), allowing to configure extra finalization steps to be executed after all the regular finalizers (represented by the `ParallelHandler` with id `urn:solid-server:default:Finalizer`). The default configurations were modified to reflect these changes. | ||
| - `config/app/init/base/init.json` defines the `CleanupInitializer`. | ||
| - `config/app/main/default.json` defines the `CleanupFinalizer`. | ||
| - `config/util/resource-locker/file.json` registers init and finalization handlers for resource lock cleanup. | ||
| - `config/util/resource-locker/redis.json` registers init and finalization handlers for resource lock cleanup. | ||
| - Configured instances of `ParallelFinalizer` were replaced with `ParallelHandler` in combination with instances of the new `FinalizableHandler` wrapper, allowing `ParallelFinalizer` to be removed and thus reducing the number of concepts. | ||
| - `config/app/identity/handler/account-store/default.json` | ||
| - `config/identity/ownership/token.json` | ||
| - `config/ldp/authorization/readers/access-checkers/agent-group.json` |
There was a problem hiding this comment.
In general I try to have short lines here to not overload the release notes so I would have just had 3 entries "Restructured the init configs", "Added lock cleanup", and "Updated finalizers" (or perhaps a few more words than that). The main question is, can what is said here also be seen clearly in the configs when looking at them? Things that can be seen by checking those shouldn't really be repeated here. E.g., when looking at the config it's clear there is a cleanup initializer now. Partially because I'm afraid people would skip over this if it's too much, but I might be wrong. Feel free to discuss though as this is quite subjective.
The paths should not start with config though, just the first folder in there so /app/init/.... The entry right above this one is wrong in that regard.
| - Both `TemplateEngine` implementations now take a `baseUrl` parameter as input. | ||
| - The `IdentityProviderFactory` and `ConvertingErrorHandler` now additionally take a `PreferenceParser` as input. | ||
| - Error handlers now take the incoming HttpRequest as input instead of just the preferences. | ||
| - Extended the initialization/finalization system: |
There was a problem hiding this comment.
Similar comment as above here. So far we have never actually put why we did things here, just what changed.
| } | ||
| }, | ||
| { | ||
| "comment": "These initializers will all be executed sequentially first when starting (the primary process) of the server.", |
There was a problem hiding this comment.
| "comment": "These initializers will all be executed sequentially first when starting (the primary process) of the server.", | |
| "comment": "Initializers that need to cleanup or do anything else before something writes to the backend should be added here.", |
| "@type": "ParallelFinalizer", | ||
| "finalizers": [ | ||
| { "@id": "urn:solid-server:default:ServerInitializer" } | ||
| "comment": "This is going to determine the sequence of finalizers that needs to be called when stopping the server.", |
There was a problem hiding this comment.
| "comment": "This is going to determine the sequence of finalizers that needs to be called when stopping the server.", | |
| "comment": "Is executed when the server is stopped.", |
The phrasing was to indicate that new entries could be added here, but this is no longer the case.
| ] | ||
| }, | ||
| { | ||
| "comment": "These finalizers will all be executed sequentially last when stopping the server.", |
There was a problem hiding this comment.
| "comment": "These finalizers will all be executed sequentially last when stopping the server.", | |
| "comment": "Finalizers that need to cleanup once no more data will be written to the backend should be added here.", |
| }); | ||
|
|
||
| afterAll(async(): Promise<void> => { | ||
| // Stop the server |
There was a problem hiding this comment.
| // Stop the server | |
| // Stop the server |
| }); | ||
|
|
||
| it('should not be affected by dangling locks.', async(): Promise<void> => { | ||
| // Simulate lock existing before server startup, by creating a (write) lock directly |
There was a problem hiding this comment.
| // Simulate lock existing before server startup, by creating a (write) lock directly | |
| // Simulate lock existing before server startup, by creating a (write) lock directly |
| @@ -0,0 +1,65 @@ | |||
| { | |||
There was a problem hiding this comment.
I assume this config can be deleted since it's not used anywhere.
| const onCompromise = | ||
| locker['defaultLockOptions'].onCompromised!; // eslint-disable-line @typescript-eslint/dot-notation |
There was a problem hiding this comment.
| const onCompromise = | |
| locker['defaultLockOptions'].onCompromised!; // eslint-disable-line @typescript-eslint/dot-notation | |
| // eslint-disable-next-line @typescript-eslint/dot-notation | |
| const onCompromise = locker['defaultLockOptions'].onCompromised!; |
Although I'm not sure why dot notation is not used?
There was a problem hiding this comment.
I did not use dot notation, because I needed a way for accessing the private property defaultLockOptions, but maybe there is a better way of doing this?
I will rewrite this section as part of the requested change for still having defaultLockOptions outside of the FileSystemResourceLocker class, but I will still require access to a private property, so tips are welcome!
There was a problem hiding this comment.
I just figured out casting to any can be an alternative solution..
📁 Related issues
#1085
✍️ Description
Code changes
Initializablein addition to the existing abstract classInitializer(which implementsAsyncHandler). This allows classes already extendingAsyncHandlerfor some other purpose, to have another entry point for initialization purposes.InitializableHandleras a wrapper class for bridging classes implementingInitializablewith the existing initializer constructs based onInitializer/AsyncHandler.Finalizerabstract class and aFinalizableHandlerwrapper class with the goal of having symmetry in the initialization and finalization mechanisms.finalizerattribute in App fromFinalizabletoFinalizerand updated the calling code inApp.stop()ParallelFinalizerasFinalizableimplementations can now be wrapped withFinalizableHandlerfor use withinParallelHandler.FileSystemResourceLockernow implementsInitializable. The existing cleanup code infinalize()was moved to a private methodclearLocks, so it can be reused in the implementation ofinitialize(). TheensureDirSyncstatement in the constructor has been replaced with a call toensureDir(async) ininitialize()as this can be seen as part of the initialization process.RedisLockernow implementsInitializable. The existing cleanup code infinalize()was moved to a private methodclearLocks, so it can be reused in the implementation ofinitialize(). An additional propertyfinalizeCalledwas added to RedisLocker that is set to true whenfinalize()is called, with the goal of preventing new Redis operations once finalization has been triggered and to throw more descriptive errors in this case.Test changes
initialize()inFileSystemResourceLocker.test.tsinitialize()inRedisLocker.test.ts. This required added mock implementations for the rediskeysanddelfunctions. Removed some test cases that no longer make sense and extended the existingfinalize()tests so that these check that keys are being cleaned up (instead of just checking thatredis.quitwas called).ResourceLockCleanup.test.tswhich tests if the persistent locking implementations (File-based and Redis-based) correctly cleanup any pre-existing locks at startup (e.g. this can happen in case of an ungraceful shutdown).Config changes
urn:solid-server:default:StartCleanupInitializeris called as the firstInitializerin theurn:solid-server:default:PrimarySequenceInitializersequence. Crucial cleanup & initialization logic that should happen before any other initialization (with the exception of logging initialization) should be added to thisSequenceHandler.urn:solid-server:default:StopCleanupFinalizeris called as the finalFinalizerstage after the existingurn:solid-server:default:Finalizer. Cleanup with consequences on the platform operations and thus that should happen after any other finalization, should be added to thisSequenceHandler.StartCleanupInitializerandStopCleanupFinalizerrespectively. This change also makes sure thatinitialize()is called if the defaultutil/resource-locker/file.jsonandutil/resource-locker/redis.jsonare imported.ParallelFinalizeras these can now be replaced withParallelHandlerin combination with wrapping the entries usingFinalizableHandler.I'm open for suggestions on better names for
StartCleanupInitializerandStopCleanupFinalizer!✅ PR check list
Before this pull request can be merged, a core maintainer will check whether