[JAVA-2582] Make predefined ReadPreferences in TaggableReadPreference public Created: 14/Aug/17  Updated: 27/Oct/23  Resolved: 08/Dec/17

Status: Closed
Project: Java Driver
Component/s: Cluster Management, Configuration
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Ruben Inoto Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to JAVA-2666 Add option for applications to regist... Closed
Backwards Compatibility: Fully Compatible

 Description   

Current Situation

The predefined ReadPreferences in TaggableReadPreference (NearestReadPreference, SecondaryPreferredReadPreference, SecondaryReadPreference) are package protected.

A developer wanting to create a new ReadPreference cannot easily extend from one of the already available ones, and it has to, either duplicate code, or to create the new ReadPreference in a com.mongodb package.

Desired Behaviour

If the predefined ReadPreferences would be public, we could easily create new ones by extending them.

Real Example

In our case, we would like to create a ReadPreference that chooses one secondary node that does not have a special tag defined. This is currently not possible with the current predefined ReadPreferences, but would be very easy by extending the SecondaryPreferredReadPreference one.



 Comments   
Comment by Jeffrey Yemin [ 04/Dec/17 ]

In scope of JAVA-2666 we've added support for application-defined server selectors, which I'm confident will satisfy this use case (so long as your application is not using a sharded MongoDB cluster, in which case extending read preferences wouldn't work either). To use this feature, your application should continue to specify ReadPreference.secondary(), and in addition it would add something like this to MongoClientOptions:

        MongoClientOptions options = MongoClientOptions.builder()
                .serverSelector(clusterDescription -> {
                    List<ServerDescription> serverDescriptionList = new ArrayList<>();
                    // the server descriptions will be pre-filtered to include only those satisfying the read preference
                    for (ServerDescription cur : clusterDescription.getServerDescriptions()) {
                        // Here's where you can exclude servers based on not having a particular tag, or whatever other logic is required
                        if (!cur.hasTags(new TagSet(new Tag("data_center", "east")))) {
                            serverDescriptionList.add(cur);
                        }
                    }
                    return serverDescriptionList;
                }).build();

JAVA-2666 will be included in the upcoming 3.6 drivers release. Let us know if you have further questions.

Generated at Thu Feb 08 08:57:35 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.