[SERVER-5112] Need a better story for startup-time initialization of C++ modules. Created: 27/Feb/12  Updated: 11/Jul/16  Resolved: 15/Oct/12

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: None
Fix Version/s: 2.3.0

Type: Improvement Priority: Major - P3
Reporter: Andy Schwerin Assignee: Andy Schwerin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by SERVER-6452 Replace time(0) calls Backlog
is depended on by CXX-90 Crash due to static initialization or... Closed
Related
related to SERVER-7332 Separate command line processing from... Closed
Participants:

 Description   

Static initializers don't work in the end, because there's no way to specify ordering dependences. The code is slowly amassing a hodgepodge of alternatives, including singleton objects initialized at first access, using a hand-written double-check locking pattern.

A better solution would be to allow programmers to declare "init functions" for modules, and to list their dependencies in their declaration. Then, at the top of main(), mongo programs would call InitMongo(), which would execute the init functions in top-sort order.

Implementations I've used in the past have declared init functions using a macro:

INIT_FN(fn_name, (input_dep_name_0, ...), (output_dep_name_0, ...)) {
  code that initializes some logical module, but cannot run until after (input_dep_name_0, ..., input_dep_name_n), and must run before (output_dep_name_0, ..., output_dep_name_n).
}

Further, you can declare nodes in the dependency graph to aggregate things. For example, if you need to run some code after all global foo objects are initialized, you can have a macro for declaring global foo objects that makes their initialization code out-depend on "foos_initialized", a noop node. Then, your code that depends on "all foos are initialized" can just have an input dependency on "foos_initialized", and doesn't need to explicitly be able to name all instances of "foo".



 Comments   
Comment by auto [ 26/Sep/12 ]

Author:

{u'date': u'2012-09-13T07:59:15-07:00', u'email': u'schwerin@10gen.com', u'name': u'Andy Schwerin'}

Message: Call runGlobalInitializersOrDie() at the top of the main() functions in mongo programs.

SERVER-5112
Branch: master
https://github.com/mongodb/mongo/commit/41c4c364d1a04de987a1aabe58b38bc4582a534e

Comment by auto [ 25/Sep/12 ]

Author:

{u'date': u'2012-09-17T09:19:26-07:00', u'email': u'schwerin@10gen.com', u'name': u'Andy Schwerin'}

Message: SERVER-5112 Introduce runGlobalInitializers(), macros and tools for global initializer declaration.

These utilities simplify adding global (process-wide) initializers that run at the beginning of
main().

Also, run the global initializers in the main() of unittests.
Branch: master
https://github.com/mongodb/mongo/commit/635aed09b0de72588422f1b864cca3810979375c

Comment by auto [ 25/Sep/12 ]

Author:

{u'date': u'2012-09-13T08:54:44-07:00', u'email': u'schwerin@10gen.com', u'name': u'Andy Schwerin'}

Message: SERVER-5112 Initializer type and test.
Branch: master
https://github.com/mongodb/mongo/commit/1c9b2a7d236cce265db1b29a708959fe87ba56ca

Comment by auto [ 25/Sep/12 ]

Author:

{u'date': u'2012-09-13T07:43:47-07:00', u'email': u'schwerin@10gen.com', u'name': u'Andy Schwerin'}

Message: Stringify the cycle into the Status message parameter when topSort detects a cycle.

Should improve reporting of programming errors involving initialization dependences.

SERVER-5112
Branch: master
https://github.com/mongodb/mongo/commit/6825307dd6534086ffbf8442eb9b5ac3cd9c13d3

Comment by auto [ 25/Sep/12 ]

Author:

{u'date': u'2012-09-11T13:59:48-07:00', u'email': u'schwerin@10gen.com', u'name': u'Andy Schwerin'}

Message: SERVER-5112 ConfigurationVariableManager type and unit tests.

In this implementation, setVariable function always takes a string
parameter, and parsing is handled by a parsing object supplied by the
configuration variable declarer.
Branch: master
https://github.com/mongodb/mongo/commit/ccd3e0333dce5bd96b80b42aaf0dc27ac3bde72a

Comment by auto [ 25/Sep/12 ]

Author:

{u'date': u'2012-09-13T07:41:38-07:00', u'email': u'schwerin@10gen.com', u'name': u'Andy Schwerin'}

Message: Rename InitializationContext to InitializerContext, for consistency with other type names.

SERVER-5112
Branch: master
https://github.com/mongodb/mongo/commit/ac4eb3fba2927f4f4c7083520cf825dbb74f0c12

Comment by auto [ 17/Sep/12 ]

Author:

{u'date': u'2012-09-10T07:35:08-07:00', u'email': u'schwerin@10gen.com', u'name': u'Andy Schwerin'}

Message: Implement and test InitializerDependencyGraph.

An InitializerDependencyGraph is a directed acyclic graph (DAG) of named
initialization operations. Every node in the graph has a unique name, a
behavior function, and a set of prerequisites. The graph supports two
inspection functions, one to get the behavior function for a node with a given
name, and the other to produce a vector of node names, ordered in a manner that
does not violate any preprequisite dependneces.

InitializerDependencyGraph is exception-free, and because it is for use very
early in process startup, it does no logging itself.

This patch also introduces a utility macro, MONGO_MAKE_STRING_VECTOR, which is
useful for constructing std::vector<std::string> from a sequence of string
literals. This patch uses it for testing, but subsequent MONGO_INIT-related
work will rely on it, as well.

Part of work on SERVER-5112.
Branch: master
https://github.com/mongodb/mongo/commit/ace5fac55b6ec2becf3758b8bdd4039438f23745

Comment by auto [ 17/Jul/12 ]

Author:

{u'date': u'2012-07-16T15:48:37-07:00', u'email': u'schwerin@10gen.com', u'name': u'Andy Schwerin'}

Message: Link programs by linking all object files on the command line.

This is done rather than enumerating a list of static libraries to include.

SERVER-5112 and SERVER-6461
Branch: master
https://github.com/mongodb/mongo/commit/78f93d7a3383b3eff72087f7b1a96930ddb59792

Comment by auto [ 17/Jul/12 ]

Author:

{u'date': u'2012-07-16T15:48:37-07:00', u'email': u'schwerin@10gen.com', u'name': u'Andy Schwerin'}

Message: Link programs by linking all object files on the command line.

This is done rather than enumerating a list of static libraries to include.

SERVER-5112 and SERVER-6461
Branch: master
https://github.com/mongodb/mongo/commit/78f93d7a3383b3eff72087f7b1a96930ddb59792

Comment by auto [ 17/Jul/12 ]

Author:

{u'date': u'2012-07-16T15:43:56-07:00', u'email': u'schwerin@10gen.com', u'name': u'Andy Schwerin'}

Message: Break a few more libraries off of "mongocommon".

Supports building all build products using _LIBDEPS=_LIBDEPS_OBJS instead of
a mix of _LIBDEPS_OBJS and _LIBDEPS_LIBS.

Necessitated in the short term by SERVER-6461, and in the long term by SERVER-5112.
Branch: master
https://github.com/mongodb/mongo/commit/da468fdddfeff6d7a22fd72a0a38a79e19eb61a3

Comment by Tad Marshall [ 07/Jun/12 ]

Another item to initialize at startup time: Windows memory allocation granularity, fetched with the GetSystemInfo() API (along with other elements). See SERVER-2942 (mongo/util/mmap_win.cpp) for usage.

Generated at Thu Feb 08 03:07:56 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.