<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:53:33 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[JAVA-991] Cannot determine actual ReplSetStatus from singleton MongoClient getReplicaSetStatus()</title>
                <link>https://jira.mongodb.org/browse/JAVA-991</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;Our application is such that the majority of it can work whenever the Mongo ReplicaSet is operating without a PRIMARY.  Recently I had a requirement to produce a HealthCheck service that reported the following states based upon the availability and capability of the Mongo ReplicaSet&lt;/p&gt;

&lt;p&gt;ReadWrite:  ReplSet is operating with a PRIMARY, GET services should 20x, PUT services should 20x&lt;br/&gt;
ReadOnly:  ReplSet is operating with only SECONDARIES, GET services should 20x, PUT services should 503&lt;br/&gt;
DOWN:  No members of the ReplSet can be reached, services should 503&lt;/p&gt;

&lt;p&gt;I was surprised at how difficult it was to write this code with the 2.11.3 Java driver.  The sticking point was that my application follows the &lt;a href=&quot;http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/&lt;/a&gt; convention of establishing a Singleton MongoClient.  The ReplicaStatus object produced by this singleton provides no methods to assert the health of anything but the master (its toString(), however, does know this).  This made it impossible to discriminate between ReadOnly and DOWN.  I need the ReplicaSetStatus produced by the Singleton MongoClient to provide a direct way to interrogate the 3 states of the ReplSet as seen by the Application Server &lt;/p&gt;
{ReadWrite, ReadOnly, DOWN}

&lt;p&gt;Here is my current ugly workaround.  I am looking for a way to drive out the whole &quot;freshClient&quot; part.&lt;/p&gt;

&lt;p&gt;    @Override&lt;br/&gt;
    public ReplSetStatus getReplSetStatus() {&lt;br/&gt;
        ReplSetStatus rss = ReplSetStatus.DOWN;&lt;br/&gt;
        MongoClient freshClient = null;&lt;br/&gt;
        try {&lt;br/&gt;
            if ( mongo != null ) {&lt;br/&gt;
                ReplicaSetStatus replicaSetStatus = mongo.getReplicaSetStatus();&lt;br/&gt;
                if ( replicaSetStatus != null ) {&lt;br/&gt;
                    if ( replicaSetStatus.getMaster() != null ) &lt;/p&gt;
{
                        rss = ReplSetStatus.ReadWrite;
                    }
&lt;p&gt; else {&lt;br/&gt;
                        /*&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;When mongo.getReplicaSetStatus().getMaster() returns null, it takes a a&lt;/li&gt;
	&lt;li&gt;fresh client to assert whether the ReplSet is read-only or completely&lt;/li&gt;
	&lt;li&gt;down. What a hack.&lt;br/&gt;
                         */&lt;br/&gt;
                        freshClient = new MongoClient( mongo.getAllAddress(), mongo.getMongoClientOptions() );&lt;br/&gt;
                        replicaSetStatus = freshClient.getReplicaSetStatus();&lt;br/&gt;
                        if ( replicaSetStatus != null ) 
{
                            rss = replicaSetStatus.getMaster() != null ? ReplSetStatus.ReadWrite : ReplSetStatus.ReadOnly;
                        }
&lt;p&gt; else &lt;/p&gt;
{
                            log.warn( &quot;freshClient.getReplicaSetStatus() is null&quot; );
                        }
&lt;p&gt;                    }&lt;br/&gt;
                } else &lt;/p&gt;
{
                    log.warn( &quot;mongo.getReplicaSetStatus() returned null&quot; );
                }
&lt;p&gt;            } else &lt;/p&gt;
{
                throw new IllegalStateException( &quot;mongo is null?!?&quot; );
            }
&lt;p&gt;        } catch ( Throwable t ) &lt;/p&gt;
{
            log.error( &quot;Ingore unexpected error&quot;, t );
        }
&lt;p&gt; finally &lt;/p&gt;
&lt;div class=&quot;error&quot;&gt;&lt;span class=&quot;error&quot;&gt;Unknown macro: {            if ( freshClient != null ) {
                freshClient.close();
            }        }&lt;/span&gt; &lt;/div&gt;
&lt;p&gt;        log.debug( &quot;getReplSetStatus(): {}&quot;, rss );&lt;br/&gt;
        return rss;&lt;br/&gt;
    }&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Alternatively, if I could as the MongoClient &quot;can you support this ReadPreference right now?&quot; or &quot;can you support this WriteConcern currently?&quot; that would work too, or maybe even better.&lt;/p&gt;

&lt;p&gt;    /**&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;@return true if current state of Client can support readPreference, false otherwise&lt;br/&gt;
     */&lt;br/&gt;
    boolean canDoRead( ReadPreference readPreference );&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;    /**&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;@return true if current state of Client can support writeConcern; false otherwise&lt;br/&gt;
     */&lt;br/&gt;
    boolean mongo.canDoWrite( WriteConcern writeConcern ) &lt;/li&gt;
&lt;/ul&gt;

</description>
                <environment></environment>
        <key id="92792">JAVA-991</key>
            <summary>Cannot determine actual ReplSetStatus from singleton MongoClient getReplicaSetStatus()</summary>
                <type id="4" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14710&amp;avatarType=issuetype">Improvement</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="2">Won&apos;t Fix</resolution>
                                        <assignee username="-1">Unassigned</assignee>
                                    <reporter username="bob_kuhar">Bob Kuhar</reporter>
                        <labels>
                    </labels>
                <created>Fri, 4 Oct 2013 19:24:54 +0000</created>
                <updated>Wed, 31 Jan 2018 19:57:05 +0000</updated>
                            <resolved>Wed, 31 Jan 2018 19:57:05 +0000</resolved>
                                                                    <component>API</component>
                                        <votes>0</votes>
                                    <watches>2</watches>
                                                                                                                <comments>
                            <comment id="1791041" author="jeff.yemin" created="Wed, 31 Jan 2018 19:57:05 +0000"  >&lt;p&gt;I don&apos;t see this as something we should address.  Even though getReplicaSetStatus presents a simple, synchronous API, it&apos;s not flexible enough to handle the wide variety of possible interrogations of the current &lt;tt&gt;ClusterDescription&lt;/tt&gt;. Since registering a &lt;tt&gt;ClusterListener&lt;/tt&gt; in &lt;tt&gt;MongoClientOptions&lt;/tt&gt; is also fairly straightforward, and allows applications to detect whatever state they are interested in, I&apos;m closing this one as Won&apos;t Fix.&lt;/p&gt;</comment>
                            <comment id="465667" author="bob_kuhar" created="Wed, 4 Dec 2013 19:20:51 +0000"  >&lt;p&gt;I can see how &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-805&quot; title=&quot;Make connection pool and connection state observable&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-805&quot;&gt;&lt;del&gt;JAVA-805&lt;/del&gt;&lt;/a&gt; would allow me to build a &quot;what state is the&lt;br/&gt;
ReplSet in now&quot; capability, but I would still like to be able to make a&lt;br/&gt;
single call to mongo.getReplicaSetStatus() and learn which of 3 states of&lt;br/&gt;
(DOWN, ReadOnly, ReadWrite) the ReplSet is in from my Singleton&lt;br/&gt;
MongoClient.  I don&apos;t see that this is part of &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-805&quot; title=&quot;Make connection pool and connection state observable&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-805&quot;&gt;&lt;del&gt;JAVA-805&lt;/del&gt;&lt;/a&gt; directly.  The&lt;br/&gt;
guiding principal should be mongo.getReplicaSetStatus() returns 1 of 3&lt;br/&gt;
states and is always current.  Today, it does not, thus &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-991&quot; title=&quot;Cannot determine actual ReplSetStatus from singleton MongoClient getReplicaSetStatus()&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-991&quot;&gt;&lt;del&gt;JAVA-991&lt;/del&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks,&lt;br/&gt;
Bob&lt;/p&gt;

</comment>
                            <comment id="465613" author="jeff.yemin" created="Wed, 4 Dec 2013 18:32:30 +0000"  >&lt;p&gt;Hi Bob,&lt;/p&gt;

&lt;p&gt;It looks like &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-805&quot; title=&quot;Make connection pool and connection state observable&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-805&quot;&gt;&lt;del&gt;JAVA-805&lt;/del&gt;&lt;/a&gt; will handle this use case.  Do you agree?&lt;/p&gt;</comment>
                    </comments>
                <issuelinks>
                            <issuelinktype id="10010">
                    <name>Duplicate</name>
                                            <outwardlinks description="duplicates">
                                        <issuelink>
            <issuekey id="71639">JAVA-805</issuekey>
        </issuelink>
                            </outwardlinks>
                                                        </issuelinktype>
                    </issuelinks>
                <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hrritj:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>74436</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>