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

Add FinishTask to FairSource #1464

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

YanzhaoW
Copy link

@YanzhaoW YanzhaoW commented Nov 17, 2023

Add an inheritance interface to FairSource such that certain actions can be taken in the end of the run.

Motivation

FairSource class may have multiple readers to analyze raw lmd data of each event (e.g. R3BUcesbSource in R3BRoot). Sometimes it's also necessary to perform certain actions in those readers at the end of the run, such as save histograms to the FileSink. Therefore, it's not suitable to do that in Close() method as some resources may be already closed.

The Reason why both Finish and FinishTask are needed:
As stated in C++ coreguidelines:

Guideline #2: Prefer to make virtual functions private.
This lets the derived classes override the function to customize the behavior as needed, without further exposing the virtual functions directly by making them callable by derived classes (as would be possible if the functions were just protected). The point is that virtual functions exist to allow customization; unless they also need to be invoked directly from within derived classes' code, there's no need to ever make them anything but private

Virtual member functions should be private unless in an interface base class. Therefore FinishTask should be private. The public interface of calling this virtual method is Finish.


Checklist:

@YanzhaoW YanzhaoW force-pushed the edwin_fairsource_finish_event branch from fc37fa3 to b600b05 Compare November 17, 2023 14:06
@YanzhaoW
Copy link
Author

@ChristianTackeGSI Something wrong in CI?

I got an error saying cmake version is too old in CI machine.

CMake Error at CMakeLists.txt:8 (cmake_minimum_required):
CMake 3.18 or higher is required. You are running version 3.17.5

@ChristianTackeGSI
Copy link
Member

@ChristianTackeGSI Something wrong in CI?

I got an error saying cmake version is too old in CI machine.

CMake Error at CMakeLists.txt:8 (cmake_minimum_required):
CMake 3.18 or higher is required. You are running version 3.17.5

We have some issues with some of our CI container images. @dennisklein wanted to update them. Seems this hasn't happened yet.

Please just ignore them. If some succeed (especially the ubuntu-rolling one), then everything is fine for now.

Copy link
Member

@ChristianTackeGSI ChristianTackeGSI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not against this. (And I like virtual functions being private!). Having hooks for things is great.

That said, I have two issues with this:

  1. The "at the end of the run" argument. If you want to do anything at the end of the run, what about deriving from FairTask a dedicated MyAtEndofRun class that overrides FairTask::Finish()?

    All the places that call GetSource()->Finish(); are preceded by fTask->FinishTask(); (which calls FairTask::Finish()). So it should be the right place for your "end of the run" idea?

    And if you want to do something "run specific", it should not be hooked to the Source. A FairSource::Finish should do something specific to the source.

  2. As you intend to derive from FairSource anyways, you could alternatively override FairSource::Close() (and call the base class' Close at the end of your Close()). So if you intend to do something about the source just before it's getting closed, that could be your place.

    (Maybe we're missing a GetSource->Close(); in FairRunAna? If so, we should probably add it?)

Can you please elaborate on your use case and why it would be better to have a Finish(Task) hook on the FairSource?

@YanzhaoW
Copy link
Author

YanzhaoW commented Nov 17, 2023

Thanks very much for your comment.

The "at the end of the run" argument. If you want to do anything at the end of the run, what about deriving from FairTask a dedicated MyAtEndofRun class that overrides FairTask::Finish()

In my case, it is the readers in the FairSource that are doing the heavy-lifting list mode data analysis instead of FairTask. Actually no FairTask is needed when reading the list mode data. So in the end, all computation results are obtained in those readers along with histograms during the analysis.

As you intend to derive from FairSource anyways, you could alternatively override FairSource::Close() (and call the base class' Close at the end of your Close()). So if you intend to do something about the source just before it's getting closed, that could be your place.

So there is a problem that when those histograms should be written to file and whether the target files are available at that moment. My first attempt is of course writing histogram during FairSource::Close() to FairRootFileSink, which requires sink file to be ready for writing at that moment. But sometimes I got segmentation fault saying sink file is not available. But in the code of FairRunOnline, FairSource::Close() is actually be called before the FairSink::Close(). So my current guess is somehow FairSource::Close() is called twice, one before FairSink::Close and another after (We had lots of double deleting. So double closing could be very possible). The one after causes the segmentation fault.

And in my opinion, this is also a design issue because there is no logic reason why FairSource::Close() must be called before FairSink::Close() and we shouldn't impose this non-trivial restriction neither. Close() function should just be used to shutdown the status of each resources and the shutdown of one resource shouldn't be dependent on other resources. If we still need to use one resource, it should be done before any shutdown starts, guaranteeing the safety of the action.

Please tell me if I need more elaboration.

Copy link
Member

@ChristianTackeGSI ChristianTackeGSI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one thing ahead: I am relatively new to FairRoot and still learning. So if I miss something, just let me know!

I think, we should have a deeper look into how Close is called for the Source.

We have a suspected double Close on the one side. And even a missed close on the other side.

So my current guess is somehow FairSource::Close() is called twice, one before FairSink::Close and another after (We had lots of double deleting. So double closing could be very possible). The one after causes the segmentation fault.

Could you have a look, if this really happens? Maybe add a LOG(DEBUG) to your Close? (would be really interesting to learn how this happens!)

I found one suspicious Close():

Comment on lines 551 to +554
{
fRootManager->StoreAllWriteoutBufferData();
fTask->FinishTask();
GetSource()->Finish();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this one is the one where I wonder, whether Close is called at all.

This is in FairRunAna::TerminateRun. And the only place I could find that calls into TerminateRun is in FairAnaSelector, which is only used in FairRunAnaProof. So it looks like this is never called. I again wonder, where GetSource()->Close() gets really called?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TerminateRun is never called (see this issue #1462)

GetSource()->Close() is only called in FairRunOnline::Finish(), which is called in FairRun::Run(). It won't be called neither if Run() is skipped.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's take a deeper look at #1462 first, please!

@ChristianTackeGSI
Copy link
Member

Hi @YanzhaoW,

are you using FairLmdSource? If so, could you take a look at #1465?

@YanzhaoW
Copy link
Author

Sure, I will check it out.

I don't know what's the best way to fix this currently. Closed function should always be used in a dtor with the correct ownership. Maybe a total re-structure?

@ChristianTackeGSI
Copy link
Member

Many thanks for taking a look! :-)

We can't call Close directly from the destructor. Calling a virtual member function from a constructor doesn't do what you would expect. So each destructor should care for the member variables / resources of its class. That's why I invented this private CloseLmd, so that this code can be reused from the destructor and Close.

That said, yes, all of this needs another look. That's why I started with #1465.

@YanzhaoW
Copy link
Author

Calling a virtual member function from a constructor doesn't do what you would expect.

You mean "destructor" instead? I don't quite understand here. If so, what kind of behaviour is unexpected?

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

Successfully merging this pull request may close these issues.

2 participants