Skip to content

Commit eeeb9ae

Browse files
author
Sam Kleinman
committed
edits to write concern tag sets
1 parent dbdc96d commit eeeb9ae

File tree

1 file changed

+135
-91
lines changed

1 file changed

+135
-91
lines changed

source/applications/replication.txt

Lines changed: 135 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ use the ``wtimeout`` argument. The following example sets the timeout to
6767

6868
db.runCommand( { getlasterror: 1, w: 2, wtimeout:5000 } )
6969

70-
Custom Write Propagation
71-
~~~~~~~~~~~~~~~~~~~~~~~~
70+
Modify Default Write Propagation Operation
71+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7272

7373
You can configure your own "default" :dbcommand:`getLastError`
7474
behavior for a replica set. Use the
@@ -99,6 +99,123 @@ have *no* other arguments.
9999
.. seealso:: :ref:`write-operations-write-concern` and
100100
:ref:`connections-write-concern`
101101

102+
Custom Write Propagation Modes
103+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104+
105+
You can use replica set tags to create custom write concerns using the
106+
:data:`~local.system.replset.settings.getLastErrorDefaults` and
107+
:data:`~local.system.replset.settings.getLastErrorModes` replica set
108+
settings.
109+
110+
.. note::
111+
112+
Custom write concern modes specify the field name and a number of
113+
*distinct* values for that field. By contrast, read preferences use
114+
the value of fields in the tag document to direct read operations.
115+
116+
In some cases, you may be able to use the same tags for read
117+
preferences and write concerns; however, you may need to create
118+
additional tags for write concerns depending on the requirements of
119+
your application.
120+
121+
Single Tag Write Concerns
122+
`````````````````````````
123+
124+
Consider a five member replica set, where each member has one of the
125+
following tag sets:
126+
127+
.. code-block:: javascript
128+
129+
{ "use": "reporting" }
130+
{ "use": "backup" }
131+
{ "use": "application" }
132+
{ "use": "application" }
133+
{ "use": "application" }
134+
135+
You could create a custom write concern mode that will ensure that
136+
applicable write operations will not return until members with two
137+
different values of the ``use`` tag have acknowledged the write
138+
operation. Create the mode with the following sequence of operations
139+
in the :program:`mongo` shell:
140+
141+
.. code-block:: javascript
142+
143+
cfg = rs.conf()
144+
cfg.settings = { getLastErrorModes: { multiUse: { "use": 2 } } }
145+
rs.reconfig(cfg)
146+
147+
.. these examples need to be better so that they avoid overwriting
148+
getLastErrorModes upon repetition (i.e. they don't $push documents
149+
to getLastErrorModes.)
150+
151+
To use this mode pass the string ``multiUse`` to the ``w`` option of
152+
:dbcommand:`getLastError` as follows:
153+
154+
.. code-block:: javascript
155+
156+
db.runCommand( { getlasterror: 1, w: multiUse } )
157+
158+
Specific Custom Write Concerns
159+
``````````````````````````````
160+
161+
If you have a three member replica with the following tag sets:
162+
163+
.. code-block:: javascript
164+
165+
{ "disk": "ssd" }
166+
{ "disk": "san" }
167+
{ "disk": "spinning" }
168+
169+
You cannot specify a custom
170+
:data:`~local.system.replset.settings.getLastErrorModes` value to
171+
ensure that the write propagates to the ``san`` before
172+
returning. However, you may implement this write concern policy by
173+
creating the following additional tags, so that the set resembles the
174+
following:
175+
176+
.. code-block:: javascript
177+
178+
{ "disk": "ssd" }
179+
{ "disk": "san", "disk.san": "san" }
180+
{ "disk": "spinning" }
181+
182+
Then, create a custom
183+
:data:`~local.system.replset.settings.getLastErrorModes` value, as
184+
follows:
185+
186+
.. code-block:: javascript
187+
188+
cfg = rs.conf()
189+
cfg.settings = { getLastErrorModes: { san: { "disk.san": 1 } } }
190+
rs.reconfig(cfg)
191+
192+
.. these examples need to be better so that they avoid overwriting
193+
getLastErrorModes upon repetition (i.e. they don't $push documents
194+
to getLastErrorModes.)
195+
196+
To use this mode pass the string ``san`` to the ``w`` option of
197+
:dbcommand:`getLastError` as follows:
198+
199+
.. code-block:: javascript
200+
201+
db.runCommand( { getlasterror: 1, w: san } )
202+
203+
This operation will not return until a replica set member with the tag
204+
``disk.san`` returns.
205+
206+
You may set a custom write concern mode as the default write concern
207+
mode using :data:`~local.system.replset.settings.getLastErrorDefaults`
208+
replica set as in the following setting:
209+
210+
.. code-block:: javascript
211+
212+
cfg = rs.conf()
213+
cfg.settings.getLastErrorDefaults = { ssd:1 }
214+
rs.reconfig(cfg)
215+
216+
.. seealso:: :ref:`replica-set-configuration-tag-sets` for further
217+
information about replica set reconfiguration and tag sets.
218+
102219
.. index:: read preference
103220
.. index:: slaveOk
104221

@@ -341,26 +458,25 @@ Tag Sets
341458
~~~~~~~~
342459

343460
Tag sets allow you to specify custom :ref:`read preferences
344-
<replica-set-read-preference>`
461+
<replica-set-read-preference>`
345462
and :ref:`write concerns <write-operations-write-concern>`
346463
so that your application can target
347-
operations to specific members, based on custom parameters.
348-
349-
.. important::
350-
351-
Custom read preferences and write concerns evaluate tags sets in
352-
different ways.
353-
354-
Read preferences consider the value of a tag when selecting a member
355-
to read from.
356-
357-
Write concerns do not utilize the value of a tag to select a
358-
member except to consider whether or not the value is unique.
359-
360-
Custom Read Preferences
361-
```````````````````````
464+
operations to specific members, based on custom parameters.
465+
466+
.. note::
467+
468+
Consider the following properties of read preferences:
362469

363-
A tag set for a read operation may resemble the following:
470+
- Custom read preferences and write concerns evaluate tags sets in
471+
different ways.
472+
473+
- Read preferences consider the value of a tag when selecting a
474+
member to read from.
475+
476+
- Write concerns ignore the value of a tag to when selecting a
477+
member *except* to consider whether or not the value is unique.
478+
479+
A tag set for a read operation may resemble the following document:
364480

365481
.. code-block:: javascript
366482

@@ -419,78 +535,6 @@ For more information on how read preference :ref:`modes
419535
<replica-set-read-preference-modes>` interact with tag sets, see the
420536
documentation for each read preference mode.
421537

422-
Custom Write Concerns
423-
`````````````````````
424-
425-
Replica set tags can be used to create custom write concerns through
426-
a combination of the tags and the :data:`~local.system.replset.settings.getLastErrorDefaults`
427-
replica set setting.
428-
429-
You may be able to utilize the same tags you have created for read
430-
preferences, however you may also need to create additional tags to
431-
fully utilize custom write concerns.
432-
433-
For example, previously you may have tagged a member with the tags
434-
:samp:`disk` and :samp:`use`:
435-
436-
.. code-block:: javascript
437-
438-
{ "disk": "ssd", "use": "reporting" }
439-
440-
In order to direct write operations to the member tagged with :samp:`disk:{ssd}`
441-
you must add an additional tag:
442-
443-
.. code-block:: javascript
444-
445-
{ "disk.ssd": "ssd"}
446-
447-
You would then use this tag to create a custom getLastErrorMode:
448-
449-
.. code-block:: javascript
450-
451-
> var cfg = rs.conf()
452-
> cfg.settings = { getLastErrorModes: { ssd: {"disk.ssd":1}} }
453-
> rs.reconfig(cfg)
454-
455-
This creates a custom getLastError mode of :samp:`ssd`, requiring
456-
at least one member tagged :samp:`ssd` to confirm the write operation.
457-
458-
If, however, you wanted multiple members tagged :samp:`ssd`
459-
to confirm the write operation, you need to tag each member with
460-
a different value for the :samp:`disk.ssd` tag.
461-
For example:
462-
463-
.. code-block:: javascript
464-
465-
> cfg.members[2].tags = {"disk.ssd": "APPLE SSD SM512E"}
466-
> cfg.members[3].tags = {"disk.ssd": "APPLE SSD SM512E"}
467-
> cfg.members[4].tags = {"disk.ssd": "INTEL SSD S3700"}
468-
469-
And your custom getLastErrorMode would be:
470-
471-
.. code-block:: javascript
472-
473-
> cfg.settings = { getLastErrorModes: { ssd: {"disk.ssd":2}} }
474-
475-
This getLastErrorMode directs MongoDB to confirm writes to two replica
476-
members tagged with unique values for the :samp:`disk.ssd` tag.
477-
In this example either :samp:`member[{2}]` or :samp:`member[{3}]` would
478-
satisfy one requirement, and :samp:`member[{4}]` would satisfy
479-
the requirement for a second member.
480-
481-
The custom write concern can be specified as you would specify a write
482-
concern or it may be added as a default write concern using the
483-
the :data:`~local.system.replset.settings.getLastErrorDefaults`
484-
replica set setting:
485-
486-
.. code-block:: javascript
487-
488-
> cf.settings.getLastErrorDefaults = {ssd:1}
489-
490-
.. seealso:: :ref:`replica-set-configuration-tag-sets` for further
491-
information about replica set reconfiguration and tag sets.
492-
493-
494538
.. index:: read preference; behavior
495539
.. _replica-set-read-preference-behavior:
496540

0 commit comments

Comments
 (0)