-
Notifications
You must be signed in to change notification settings - Fork 0
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
[Draft] Components #3
Comments
Now I can return to Andrey's notes about the current realization of components:
Speaking about component objects, it is useful to separate the interface and the implementation of a component for the following reasons:
Speaking about component modules, you are correct that we can put an interface and the main implementation of the same component into the same module. But having different modules for interface and implementation is helpful for two reasons:
As I already wrote above, I disagree with this. In the current implementation, interfaces describe component fields in a strongly typed way. Inside your component, you can write Let me compare the current way how components work with the previous iteration of the component architecture. On the previous iteration, components indeed do not have interfaces. Each component had a single value which was provided by that component: class TunnelsComponent(Component):
role = TUNNELS_COMMUNITY
...
async def run(self, mediator):
await super().run(mediator)
config = mediator.config
ipv8 = await self.use(mediator, IPV8_SERVICE)
bandwidth_community = await self.use(mediator, BANDWIDTH_ACCOUNTING_COMMUNITY)
peer = await self.use(mediator, MY_PEER)
dht_community = await self.use(mediator, DHT_DISCOVERY_COMMUNITY)
download_manager = await self.use(mediator, DOWNLOAD_MANAGER)
bootstrapper = await self.use(mediator, IPV8_BOOTSTRAPPER)
rest_manager = await self.use(mediator, REST_MANAGER) In my opinion, this approach had the following drawbacks:
With component interfaces, we now have typed values, and a single component can hold more than one value. Now the same code looks like this: class TunnelsComponentImp(TunnelsComponent):
async def run(self):
await self.use(ReporterComponent, required=False)
config = self.session.config
ipv8_component = await self.use(Ipv8Component)
ipv8 = ipv8_component.ipv8
peer = ipv8_component.peer
dht_discovery_community = ipv8_component.dht_discovery_community
bandwidth_component = await self.use(BandwidthAccountingComponent, required=False)
bandwidth_community = bandwidth_component.community if bandwidth_component.enabled else None
download_component = await self.use(LibtorrentComponent, required=False)
download_manager = download_component.download_manager if download_component.enabled else None
rest_component = await self.use(RESTComponent, required=False)
rest_manager = rest_component.rest_manager if rest_component.enabled else None It does not look more concise because the code now takes into account that some optional components can be disabled in the Tribler configuration. But you can see the following differences:
As I wrote above, in my opinion, this is confusion caused by an unfortunate naming.
Eventually, we should probably merge components and modules into a single folder. But right now, we don't have a one-to-one mapping between components and files in the In previous iteration of components architecture, components reside in the modules directory. The directory structure looked like this (a subset of files and subdirectories):
Was this directory layout more convenient as compared to having all components in a dedicated directory? To me, it wasn't, for the following reasons:
Having component implementations in a separate folder solves these two problems:
But in return, you need to have one more separate directory. Each layout has some benefits and drawbacks. So, choosing the directory layout is a trade-off. We need to discuss it and choose a less annoying layout that has the least number of drawbacks. Then we can switch to the most desired layout quickly, with a simple IDE-supported refactoring.
Agree with that. I need to add comments and probably some internal documentation. Probably descriptions from the current thread can be used as a start for it. In conclusion, I think that the current implementation of components has its warts. I usually don't like to have a big number of files and directories, as well as boilerplate code. I hope we can improve the current architecture after the discussion. But we need to have a common understanding of component architecture goals, possible trade-offs, and viable alternatives. Hopefully, with the forthcoming meeting, we can achieve this. |
Added tests for snippet generation when searching
I'm excited about the prospect of an offline all-dev-team meeting for discussing components. I'm sure that with the collective discussion, we can make the components architecture much better.
The current version of the components architecture has not emerged from an empty initial state. It was an incremental refactoring of the previous approach that you can see here.
First, I want to describe the component architecture that we have right now. The current implementation of components is trying to achieve the following goals:
Let's see how the current components implementation achieves that.
Static typing of components
With the current approach, a developer can implement a component interface, which statically describes all fields provided by a component. It is possible to implement methods in a component interface. Still, usually, it is just some other objects provided by a component, such as a community, a store, or a manager implemented by some Tribler module.
A component is treated as a container for some values (such as communities) which are statically typed. A component interface specifies field types, while the component implementation provides the actual values of these fields.
Some other component's implementation can specify its dependency on
FooComponent
in its implementation:If your PyCharm is configured correctly and you try to play with current components in PyCharm IDE, you will see that PyCharm can perfectly understand that the result of the
self.use(FooComponent)
call has theFooComponent
type. It is beneficial, as it allows PyCharm and various type checkers to highlight many errors early on. You can navigate easily through the code by clicking on a component field likefoo_component.manager
. That should bring you to the right place, where you can click on the field type to see theDownloadManager
class definition. Also, you can click onFooComponentImp
and see all details of theDownloadManager
instance initialization.So, in responding to:
I disagree with that. Interfaces in the current implementation of components are real interfaces, as they:
I need to say that the component's interfaces in the current implementation are not pure interfaces, as, in addition to the Interface pattern, they also implement the Factory pattern. In the current component architecture, each component's interface and its implementation are coupled: each interface is linked to a single "real" implementation and a single "dummy" implementation. This is done for simplification: no Tribler interface needs to have more than one "real" implementation.
Flexible configuration of Tribler
Our current component architecture allows us to use a subset of components. Depending on configuration options, some components can be turned off. For this, a class of component's interface needs to provide
should_be_enabled
classmethod:Again, the reason to place this classmethod in the component's interface class was to simplify architecture as much as possible and don't introduce unnecessary new entities. In the current Tribler architecture, it is enough to have this method hardcoded inside the component interface. We can extract it later when (and if) a reason for this will appear.
It is possible that other components are trying to use component which is not currently enabled:
What should be the value of the
foo_component
variable in that case? One option was to set it toNone
. But it turns out to be pretty inconvenient. Currently, a component is basically a container for some fields which are initialized indirectly in a Dependency Injection style. We don't use the component itself. We use fields that this component provides to us, like in the following example:If
foo_component
can beNone
, then direct access tofoo_component.community
and other fields becomes impossible - we need to check iffoo_component is None
or not before that. In practice, it turns out to be inconvenient and error-prone.In order to avoid this, each component has a second dummy implementation which is returned when the component is disabled. This dummy implementation has the same set of fields as the real component implementation, so we can access the
foo_component.community
attribute even iffoo_component
is actually disabled. The actual value offoo_component.community
for dummy implementation ofFooComponent
can beNone
or some dummy version of the community depending on what is more convenient for other components, which depends onFooComponent
.Currently, the class for dummy
FooComponent
implementation is namedFooComponentMock
, but the name is actually pretty misleading. TheMock
ending gives the impression that this class is for tests. It actually can be used for tests, especially if we are testing some other component and want to disable everything that is not relevant to the current test. But the main purpose of this class is to be used in real Tribler run when the component was disabled in configuration settings, but other components can attempt to use it. For that reason, I think that it is better to rename dummy components implementations fromFooComponentMock
toDummyFooComponent
or something like that.An attribute
foo_component.enabled
allows us to understand whether we have an actual component implementation or its dummy version. This allows us to write code in the following style:How components solve the problem of circular imports
This part is straightforward. Each component's implementation refers to interfaces of other components, but not to their implementation:
This way, components can link each other easily. Cross-dependencies are possible and will not cause any problems during the module import.
The same is with optional component dependencies. Let's consider the situation when
BarComponent
has a dependency onFooComponent
, but that dependency is optional, soBarComponent
can work fine ifFooComponent
is disabled in config settings. ThenBarComponent
can be written this way:If
FooComponent
is disabled, thenBarComponent
will loadFooComponent
interface, but not the implementation. If the implementation ofFooComponent
contains imports of some expensive modules, this time will not be spent, and the Tribler startup time will be reduced.Direct communication of components
The result of it was a highly coupled architecture when all possible "components" should be specified in Session. It kind of works but might be error-prone when not all "components" are actually enabled.
In the new component architecture, the components don't use the
Session
object at all to communicate with each other. Instead, they directly use other components in their code:This made component dependencies clean and explicit, and it became possible to understand which components are using other components easily.
Asynchronous initialization of components
There are some tricky questions regarding components initialization:
The simplest solution would be to specify some strict linear order for component initialization and then initialize components in that order one by one. This approach is possible but has two big drawbacks:
In the current architecture, another approach was implemented, which allows all components to be initialized asynchronously and in parallel. If some component A refers to another component B, the initialization process of component A is paused until component B becomes fully initialized. Then component A receives the reference to component B, and the initialization process of component A resumes. You can see it in the following example:
A similar logic is applied to the component shutdown - a component finalization procedure starts when all other components that depend on this component were already stopped.
With the current implementation, it is possible to have a graph of dependencies between components to detect cyclic dependencies during component initialization. It was already implemented but then removed to simplify code a bit, as it was unnecessary to have this detection mechanism. It can be added again afterward.
Assembling arbitrary configurations of components
In the current component architecture, the role of
Session
was heavily reduced.Previously the session object was the central place where references to all important "components" were stored, and many "components" access the session object to find references to other "components". Now components can talk to each other directly without the direct use of the
Session
object.Nevertheless, the Session object remains an important part of the architecture. Its main purpose now is to keep the mapping of component interfaces to component implementations.
As I said previously, now each component interface has two implementations - one is "real" implementation which is used in the normal Tribler run, and another is "dummy" implementation which is used when the component is disabled.
But in tests, it may be important to replace some specific components with mock implementations. I'm speaking not about classes like
FooComponentMocks
, that really should be renamed to something likeDummyFooComponent
, and that is used in the real application when the corresponding component is disabled, but about some test-specific mocks which can be implemented differently in different tests.Session
object in a new component architecture can assemble an arbitrary collection of component implementations in the following way:Here,
foo_mock
is some mock implementation that inherits from theFooComponent
interface. It should have aninterface
attribute that refers to that interface. Then the code of thebar_component
will be executed:This way, the current component architecture allows you to test arbitrary assemblies of components with arbitrary implementation for each component.
The text was updated successfully, but these errors were encountered: