[SERVER-31133] Add capability for ignores on the `StatusWith`'s `Status` member Created: 18/Sep/17  Updated: 30/Oct/23  Resolved: 06/Dec/17

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 3.7.1

Type: Improvement Priority: Minor - P4
Reporter: ADAM Martin (Inactive) Assignee: ADAM Martin (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Sprint: Platforms 2017-12-04, Platforms 2017-12-18
Participants:

 Description   

There are legitimate cases where developers wish to ignore the results of a `StatusWith` returning call. An example is `TaskExecutor::scheduleRemoteCommand`, which returns a `StatusWith< CallbackHandle >` result.

A common use case which benefits from this change is one which needs to schedule background or remote "kill" style commands on that interface during some kind of destruction or shutdown. If the creation of the remote request fails, it doesn't matter, as the request was part of some kind of shutdown (often in case of failure). If the request was successful, the developer doesn't need the actual value result of the `StatusWith`, as there is no need to wait for this operation to complete.

Presently, `StatusWith` has a member `getStatus` which returns the `Status` member by const reference. This means that constructions such as these are necessary to avoid problems:

Status s= executor->scheduleRemoteCommand( request, []( auto ) {} ).getStatus();
std::move( s ).ignore();

or

[]{ return executor->scheduleRemoteCommand( request, []( auto ) {} ).getStatus(); }().ignore();

or

auto result=executor->scheduleRemoteCommand( request, []( auto ) {} ).getStatus();
if( !executor->scheduleRemoteCommand( request, []( auto ) {} ).getStatus().isOK() ) { (void) 0; }

or other similar ugly constructions.

`StatusWith` deliberately lacks an `ignore` member, as it would be unclear to the reader which part of a `StatusWith` was being ignored – the actual return value, or the error-encoding result. Permitting `Status::ignore` to be called from the results of a `StatusWith< T >::getStatus()` call would greatly improve readability:

executor->scheduleRemoteCommand( request, []( auto ) {} ).getStatus().ignore();

This is fairly trivial to implement. I already have a prototype, and a number of people have requested this kind of mechanism.



 Comments   
Comment by Githook User [ 06/Dec/17 ]

Author:

{'name': 'ADAM David Alan Martin', 'username': 'adamlsd', 'email': 'adam.martin@10gen.com'}

Message: SERVER-31133 Ignore mechanism for `StatusWith`

Change `StatusWith< T >::getStatus` to return `const Status &&`, which
permits an overload for `Status::ignore() const &&`. This would be
called from expressions which use `StatusWith< T >::getStatus()`
for example, `f().getStatus().ignore()`, giving a straightforward
and concicse way to indicate that the success or failure of an operation
is irrelevant (and thus the result is also irrelevant).
Branch: master
https://github.com/mongodb/mongo/commit/e2733b633fbd06b380c4f0febca3e2f33e93dba5

Generated at Thu Feb 08 04:26:06 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.