[JAVA-1672] Regression applying read preference with tag set Created: 04/Mar/15  Updated: 01/Apr/16  Resolved: 05/Mar/15

Status: Closed
Project: Java Driver
Component/s: Cluster Management
Affects Version/s: 3.0.0
Fix Version/s: 3.0.0

Type: Bug Priority: Major - P3
Reporter: Borisa Zivkovic Assignee: Unassigned
Resolution: Done Votes: 0
Labels: rc
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

3 centos machines

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.0-beta3</version>
</dependency>

mongodb-linux-x86_64-3.0.0-rc11.tgz



 Description   

I tried using replica sets with tagging and then to redirect reads from client to appropriate secondary with given tags.

I followed instructions given http://docs.mongodb.org/manual/tutorial/configure-replica-set-tag-sets/ and http://docs.mongodb.org/ecosystem/drivers/java-replica-set-semantics/

I have tag secondary:0 and secondary:1 - and want my clients to load-balance between those two secondary nodes when reading data (query + aggregations)

but I always get following error

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=secondary: [TagSet{[Tag{name='secondary', value='0'}]}]}. Client view of cluster state is {type=REPLICA_SET, servers=[{address=host3:27019, type=REPLICA_SET_SECONDARY, TagSet{[Tag{name='test', value='BsonString{value='a'}'}, Tag{name='secondary', value='BsonString{value='0'}'}]}, roundTripTime=1.8 ms, state=CONNECTED}, {address=host2:27019, type=REPLICA_SET_SECONDARY, TagSet{[Tag{name='test', value='BsonString{value='b'}'}, Tag{name='secondary', value='BsonString{value='0'}'}]}, roundTripTime=1.8 ms, state=CONNECTED}, {address=host1:27019, type=REPLICA_SET_PRIMARY, TagSet{[Tag{name='secondary', value='BsonString{value='a'}'}]}, roundTripTime=1.7 ms, state=CONNECTED}]

Here is my rs.conf and rs.status

rs0:PRIMARY> rs.status()
{
	"set" : "rs0",
	"date" : ISODate("2015-03-04T19:28:57.182Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 0,
			"name" : "host1:27019",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 42569,
			"optime" : Timestamp(1425485080, 1),
			"optimeDate" : ISODate("2015-03-04T16:04:40Z"),
			"electionTime" : Timestamp(1425454776, 1),
			"electionDate" : ISODate("2015-03-04T07:39:36Z"),
			"configVersion" : 8,
			"self" : true
		},
		{
			"_id" : 1,
			"name" : "host2:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 42560,
			"optime" : Timestamp(1425485080, 1),
			"optimeDate" : ISODate("2015-03-04T16:04:40Z"),
			"lastHeartbeat" : ISODate("2015-03-04T19:28:55.471Z"),
			"lastHeartbeatRecv" : ISODate("2015-03-04T19:28:56.631Z"),
			"pingMs" : 0,
			"syncingTo" : "host1:27019",
			"configVersion" : 8
		},
		{
			"_id" : 2,
			"name" : "host3:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 42552,
			"optime" : Timestamp(1425485080, 1),
			"optimeDate" : ISODate("2015-03-04T16:04:40Z"),
			"lastHeartbeat" : ISODate("2015-03-04T19:28:55.661Z"),
			"lastHeartbeatRecv" : ISODate("2015-03-04T19:28:55.343Z"),
			"pingMs" : 0,
			"syncingTo" : "host2:27019",
			"configVersion" : 8
		}
	],
	"ok" : 1
}

rs0:PRIMARY> rs.conf();
{
	"_id" : "rs0",
	"version" : 8,
	"members" : [
		{
			"_id" : 0,
			"host" : "host1:27019",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				"secondary" : "a"
			},
			"slaveDelay" : 0,
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "host2:27019",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				"secondary" : "0",
				"test" : "a"
			},
			"slaveDelay" : 0,
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "host3:27019",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				"secondary" : "0",
				"test" : "b"rs0:PRIMARY> rs.status()
{
	"set" : "rs0",
	"date" : ISODate("2015-03-04T19:28:57.182Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 0,
			"name" : "host1:27019",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 42569,
			"optime" : Timestamp(1425485080, 1),
			"optimeDate" : ISODate("2015-03-04T16:04:40Z"),
			"electionTime" : Timestamp(1425454776, 1),
			"electionDate" : ISODate("2015-03-04T07:39:36Z"),
			"configVersion" : 8,
			"self" : true
		},
		{
			"_id" : 1,
			"name" : "host2:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 42560,
			"optime" : Timestamp(1425485080, 1),
			"optimeDate" : ISODate("2015-03-04T16:04:40Z"),
			"lastHeartbeat" : ISODate("2015-03-04T19:28:55.471Z"),
			"lastHeartbeatRecv" : ISODate("2015-03-04T19:28:56.631Z"),
			"pingMs" : 0,
			"syncingTo" : "host1:27019",
			"configVersion" : 8
		},
		{
			"_id" : 2,
			"name" : "host3:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 42552,
			"optime" : Timestamp(1425485080, 1),
			"optimeDate" : ISODate("2015-03-04T16:04:40Z"),
			"lastHeartbeat" : ISODate("2015-03-04T19:28:55.661Z"),
			"lastHeartbeatRecv" : ISODate("2015-03-04T19:28:55.343Z"),
			"pingMs" : 0,
			"syncingTo" : "host2:27019",
			"configVersion" : 8
		}
	],
	"ok" : 1
}

rs0:PRIMARY> rs.conf();
{
	"_id" : "rs0",
	"version" : 8,
	"members" : [
		{
			"_id" : 0,
			"host" : "host1:27019",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				"secondary" : "a"
			},
			"slaveDelay" : 0,
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "host2:27019",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				"secondary" : "0",
				"test" : "a"
			},
			"slaveDelay" : 0,
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "host3:27019",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				"secondary" : "0",
				"test" : "b"
			},
			"slaveDelay" : 0,
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatTimeoutSecs" : 10,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		}
	}
}
rs0:PRIMARY> 
 
			},
			"slaveDelay" : 0,
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatTimeoutSecs" : 10,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		}
	}
}
rs0:PRIMARY> 



 Comments   
Comment by Jeffrey Yemin [ 31/Mar/15 ]

Closing all resolved 3.0.0 issues, as 3.0.0 has been tagged and released.

Comment by Githook User [ 05/Mar/15 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: Fixed bug converting tags document from ismaster result to TagSet

JAVA-1672
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/452df6659b2597c22d52eb28e5757312011403f3

Comment by Borisa Zivkovic [ 04/Mar/15 ]

Relevant Java code:

public static MongoCollection<Document> getCollection(String secondaryId) {
		MongoDatabase database = getDatabase(secondaryId);
		MongoCollection<Document> coll = database.getCollection("benchdocs").withWriteConcern(WriteConcern.ACKNOWLEDGED).withReadPreference(getReadPreference(secondaryId));
		return coll;
	}
 
	private static MongoDatabase getDatabase(String secondaryId) {
		MongoDatabase database = getClient().getDatabase("mongobench").withReadPreference(getReadPreference(secondaryId));
		return database;
	}
 
	private static ReadPreference getReadPreference(String secondaryId) {
		boolean readFromReplicas = "true".equalsIgnoreCase(System.getProperty("readFromReplicas"));
		if (readFromReplicas) {
			boolean enableTags = "true".equalsIgnoreCase(System.getProperty("enableTags"));
			if (enableTags && secondaryId != null) {
				Tag t = new Tag("secondary", secondaryId);
				List<Tag> tags = new LinkedList<Tag>();
				tags.add(t);
				TagSet ts = new TagSet(tags);
				return ReadPreference.secondary(ts);
			} else {
				return ReadPreference.secondary();
			}
		}
		return ReadPreference.primary();
	}
 
	private static MongoClient getClient() {
		if (client == null) {
			String mongoServer = "localhost";
			String confMongoServer = System.getProperty(MONGO_HOST_ADDRESS_PROP_NAME);
			if (confMongoServer != null && confMongoServer.trim().length() > 0) {
				mongoServer = confMongoServer;
			}
			System.out.println("Will connecto to MongoDB at address " + mongoServer);
			ServerAddress sa = new ServerAddress(mongoServer, getPort());
			List<ServerAddress> servers = new LinkedList<ServerAddress>();
			servers.add(sa);
			MongoClient mongoClient = new MongoClient(servers);
			mongoClient.setWriteConcern(WriteConcern.ACKNOWLEDGED);
			mongoClient.setReadPreference(ReadPreference.secondary());
			client = mongoClient;
		}
		return client;
	}

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