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

Move time source from db layer to PersistenceManager for better separation of concerns #6610

Open
bowenxia opened this issue Jan 10, 2025 · 2 comments
Labels
up-for-grabs Issues that are good entry points for those new to Cadence that want to contribute

Comments

@bowenxia
Copy link
Member

Is your feature request related to a problem? Please describe.
Currently, the timeSrc field is being handled at the DB layer, but this implementation has raised some concerns about its correctness and responsibility allocation. Specifically, the timestamp should ideally be determined at the PersistenceManager level rather than the DB layer, which should solely focus on translating entity records into database queries without adding additional logic.

Problem Statement
The timeSrc field is currently being used within the DB layer to determine timestamps, including during query generation.
While this approach enables unit test validation by replacing time.Now() with a mockable db.timeSrc.Now(), it introduces a mixing of responsibilities.
The proper handling of timestamps should occur at the PersistenceManager level, ensuring better separation of concerns and adherence to the single responsibility principle.

Proposed Solution
Refactor the system so that the PersistenceManager level handles the timeSrc field.
The DB layer should only translate entity records into database queries without modifying or adding any additional information like timestamps.
This would involve removing the timestamp logic from the DB layer and ensuring it is fully managed by the upper layers.

Additional context
See discussion in this PR: #6593

@bowenxia bowenxia added the up-for-grabs Issues that are good entry points for those new to Cadence that want to contribute label Jan 10, 2025
@taylanisikdemir taylanisikdemir changed the title Refactor timeSrc Responsibility to PersistenceManager for Better Separation of Concerns Move time source from db layer to PersistenceManager for better separation of concerns Jan 10, 2025
@ribaraka
Copy link

Hello! I’d like to work on this task. I have a couple of questions so far.
Could you confirm that the PersistenceManager is simply an abstract concept referring to a higher-level service (or layer) that orchestrates persistence operations? I ask because the term is written together, which makes it a bit confusing 🥲 And I found that there is indeed a component called PersistenceManager in the iWF project, but I want to know that this term has no relation to the iWF. Apologies if this question seems trivial.

Regarding the logic for generating timestamps: Based on this discussion, the InsertReplicationTask method should not generate a new timestamp (i.e, created_time), instead, this responsibility lies with the PersistenceManager 🥲 . I also found that the required timestamp is already provided in the persistence.InternalReplicationTaskInfo struct via the CreationTime field. To confirm, in cases like this, I won’t regenerate the current time but will use the existing timestamp provided in the struct. Is this correct? Additionally, if this approach applies to similar cases, can I assume it’s best practice to avoid generating new timestamps and instead rely on existing ones where available?

Thank you for your guidance! 💪

@ribaraka
Copy link

New Development:
I proposed an approach earlier regarding handling the timeStamp in the InsertReplicationTask method. However, how should this be addressed in other methods? For instance, the InsertWorkflowExecutionWithTasks method has a similar parameter (replicationTasks []*nosqlplugin.ReplicationTask), which can be empty in some cases.
Relying on replicationTasks to extract a timeStamp in one method (e.g., InsertReplicationTask) while using a different field in another method (e.g., InsertWorkflowExecutionWithTasks) feels inconsistent and unstructured. If this acceptable, we can proceed with this.

Another approach I considered is introducing a new parameter to these methods for handling the current timeStamp. However, this would disrupt the existing interfaces, which isn’t ideal. If this approach is preferable I could go with this one, but probably I should think about maintaining backward compatibility with existing interfaces (input parameters for methods) if it is a concern.

A third option is to pass the current time directly into the Context. This might be a cleaner and more consistent solution, but I’m uncertain if it would misuse the concept of context. If it does, still, are you okay moving forward with this?

Sorry for asking so many questions upfront, I want to ensure I have a solid plan of how to proceed. If you have a better approach, please share with me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
up-for-grabs Issues that are good entry points for those new to Cadence that want to contribute
Projects
None yet
Development

No branches or pull requests

2 participants