brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [5/9] git commit: Fix MongoDBReplicaSetIntegrationTest
Date Fri, 31 Oct 2014 14:10:10 GMT
Fix MongoDBReplicaSetIntegrationTest

- need to retry addMember/removeMember if first attempt failed
  (e.g. because primary was not yet stable, or attributes of
  primary were temporarily out-of-sync)
- code tidy of MongoDBReplicaSetIntegrationTest


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/636df65d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/636df65d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/636df65d

Branch: refs/heads/master
Commit: 636df65d7a53d70579c015fe87bd48592c1d8a1b
Parents: cddf4bc
Author: Aled Sage <aled.sage@gmail.com>
Authored: Fri Oct 31 10:43:05 2014 +0000
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Fri Oct 31 14:08:03 2014 +0000

----------------------------------------------------------------------
 .../nosql/mongodb/MongoDBReplicaSetImpl.java    | 37 ++++++++++++++++----
 .../entity/nosql/mongodb/MongoDBServerImpl.java |  6 ++++
 .../MongoDBReplicaSetIntegrationTest.java       | 30 ++++++----------
 3 files changed, 48 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/636df65d/software/nosql/src/main/java/brooklyn/entity/nosql/mongodb/MongoDBReplicaSetImpl.java
----------------------------------------------------------------------
diff --git a/software/nosql/src/main/java/brooklyn/entity/nosql/mongodb/MongoDBReplicaSetImpl.java
b/software/nosql/src/main/java/brooklyn/entity/nosql/mongodb/MongoDBReplicaSetImpl.java
index b1e2393..6d09b4c 100644
--- a/software/nosql/src/main/java/brooklyn/entity/nosql/mongodb/MongoDBReplicaSetImpl.java
+++ b/software/nosql/src/main/java/brooklyn/entity/nosql/mongodb/MongoDBReplicaSetImpl.java
@@ -224,17 +224,28 @@ public class MongoDBReplicaSetImpl extends DynamicClusterImpl implements
MongoDB
                 // SERVICE_UP is not guaranteed when additional members are added to the
set.
                 Boolean isAvailable = secondary.getAttribute(MongoDBServer.SERVICE_UP);
                 MongoDBServer primary = getPrimary();
+                boolean reschedule;
                 if (Boolean.TRUE.equals(isAvailable) && primary != null) {
-                    primary.addMemberToReplicaSet(secondary, nextMemberId.incrementAndGet());
-                    if (LOG.isInfoEnabled()) {
+                    boolean added = primary.addMemberToReplicaSet(secondary, nextMemberId.incrementAndGet());
+                    if (added) {
                         LOG.info("{} added to replica set {}", secondary, getName());
+                        reschedule = false;
+                    } else {
+                        if (LOG.isDebugEnabled()) {
+                            LOG.debug("{} could not be added to replica set via {}; rescheduling",
secondary, getName());
+                        }
+                        reschedule = true;
                     }
                 } else {
                     if (LOG.isTraceEnabled()) {
                         LOG.trace("Rescheduling addition of member {} to replica set {}:
service_up={}, primary={}",
-                            new Object[]{secondary, getName(), isAvailable, primary});
+                            new Object[] {secondary, getName(), isAvailable, primary});
                     }
-                    // Could limit number of retries
+                    reschedule = true;
+                }
+                
+                if (reschedule) {
+                    // TODO Could limit number of retries
                     executor.schedule(this, 3, TimeUnit.SECONDS);
                 }
             }
@@ -264,16 +275,30 @@ public class MongoDBReplicaSetImpl extends DynamicClusterImpl implements
MongoDB
                 Boolean isAvailable = member.getAttribute(MongoDBServer.SERVICE_UP);
                 // Wait for the replica set to elect a new primary if the set is reconfiguring
itself.
                 MongoDBServer primary = getPrimary();
+                boolean reschedule;
+                
                 if (primary != null && !isAvailable) {
-                    primary.removeMemberFromReplicaSet(member);
-                    if (LOG.isInfoEnabled()) {
+                    boolean removed = primary.removeMemberFromReplicaSet(member);
+                    if (removed) {
                         LOG.info("Removed {} from replica set {}", member, getName());
+                        reschedule = false;
+                    } else {
+                        if (LOG.isDebugEnabled()) {
+                            LOG.debug("{} could not be removed from replica set via {}; rescheduling",
member, getName());
+                        }
+                        reschedule = true;
                     }
+
                 } else {
                     if (LOG.isTraceEnabled()) {
                         LOG.trace("Rescheduling removal of member {} from replica set {}:
service_up={}, primary={}",
                             new Object[]{member, getName(), isAvailable, primary});
                     }
+                    reschedule = true;
+                }
+                
+                if (reschedule) {
+                    // TODO Could limit number of retries
                     executor.schedule(this, 3, TimeUnit.SECONDS);
                 }
             }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/636df65d/software/nosql/src/main/java/brooklyn/entity/nosql/mongodb/MongoDBServerImpl.java
----------------------------------------------------------------------
diff --git a/software/nosql/src/main/java/brooklyn/entity/nosql/mongodb/MongoDBServerImpl.java
b/software/nosql/src/main/java/brooklyn/entity/nosql/mongodb/MongoDBServerImpl.java
index e1e6c5f..7aa992d 100644
--- a/software/nosql/src/main/java/brooklyn/entity/nosql/mongodb/MongoDBServerImpl.java
+++ b/software/nosql/src/main/java/brooklyn/entity/nosql/mongodb/MongoDBServerImpl.java
@@ -177,6 +177,12 @@ public class MongoDBServerImpl extends SoftwareProcessImpl implements
MongoDBSer
 
     @Override
     public boolean addMemberToReplicaSet(MongoDBServer secondary, Integer id) {
+        // TODO The attributes IS_PRIMARY_FOR_REPLICA_SET and REPLICA_SET_MEMBER_STATUS can
be out-of-sync.
+        // The former is obtained by an enricher that listens to STATUS_BSON (set by client.getServerStatus()).
+        // The latter is set by a different feed doing client.getReplicaSetStatus().getInt("myState").
+        // The ReplicaSet uses REPLICA_SET_MEMBER_STATUS to determine which node to call.
+        // 
+        // Relying on caller to respect the `false` result, to retry.
         if (!getAttribute(IS_PRIMARY_FOR_REPLICA_SET)) {
             LOG.warn("Attempted to add {} to replica set at server that is not primary: {}",
secondary, this);
             return false;

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/636df65d/software/nosql/src/test/java/brooklyn/entity/nosql/mongodb/MongoDBReplicaSetIntegrationTest.java
----------------------------------------------------------------------
diff --git a/software/nosql/src/test/java/brooklyn/entity/nosql/mongodb/MongoDBReplicaSetIntegrationTest.java
b/software/nosql/src/test/java/brooklyn/entity/nosql/mongodb/MongoDBReplicaSetIntegrationTest.java
index 89e66bc..948a5c4 100644
--- a/software/nosql/src/test/java/brooklyn/entity/nosql/mongodb/MongoDBReplicaSetIntegrationTest.java
+++ b/software/nosql/src/test/java/brooklyn/entity/nosql/mongodb/MongoDBReplicaSetIntegrationTest.java
@@ -22,51 +22,43 @@ import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNotEquals;
 import static org.testng.Assert.assertNotNull;
-import groovy.time.TimeDuration;
 
 import java.util.Collection;
+import java.util.concurrent.TimeUnit;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import brooklyn.entity.BrooklynAppLiveTestSupport;
 import brooklyn.entity.Entity;
-import brooklyn.entity.basic.ApplicationBuilder;
-import brooklyn.entity.basic.Entities;
 import brooklyn.entity.group.DynamicCluster;
 import brooklyn.entity.proxying.EntitySpec;
 import brooklyn.entity.trait.Startable;
 import brooklyn.location.basic.LocalhostMachineProvisioningLocation;
 import brooklyn.test.Asserts;
-import brooklyn.test.entity.TestApplication;
+import brooklyn.util.time.Duration;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.mongodb.DBObject;
 
-public class MongoDBReplicaSetIntegrationTest {
+public class MongoDBReplicaSetIntegrationTest extends BrooklynAppLiveTestSupport {
 
     @SuppressWarnings("unused")
     private static final Logger log = LoggerFactory.getLogger(MongoDBReplicaSetIntegrationTest.class);
     
-    private TestApplication app;
-    private Collection<LocalhostMachineProvisioningLocation> localhostMachineProvisioningLocation;
+    private Collection<LocalhostMachineProvisioningLocation> locs;
 
     // Replica sets can take a while to start
-    private static final TimeDuration TIMEOUT = new TimeDuration(0, 0, 180, 0);
+    private static final Duration TIMEOUT = Duration.of(3, TimeUnit.MINUTES);
 
     @BeforeMethod(alwaysRun=true)
-    public void setUp() {
-        LocalhostMachineProvisioningLocation location = new LocalhostMachineProvisioningLocation();
-        localhostMachineProvisioningLocation = ImmutableList.of(location);
-        app = ApplicationBuilder.newManagedApp(TestApplication.class);
-    }
-
-    @AfterMethod(alwaysRun=true)
-    public void tearDown() {
-        if (app != null) Entities.destroyAll(app.getManagementContext());
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        locs = ImmutableList.of(app.newLocalhostProvisioningLocation());
     }
 
     /**
@@ -81,7 +73,7 @@ public class MongoDBReplicaSetIntegrationTest {
                 .configure("memberSpec", EntitySpec.create(MongoDBServer.class)
                         .configure("mongodbConfTemplateUrl", "classpath:///test-mongodb.conf")
                         .configure("port", "27017+")));
-        app.start(localhostMachineProvisioningLocation);
+        app.start(locs);
 
         Asserts.succeedsEventually(ImmutableMap.of("timeout", TIMEOUT), new Runnable() {
             @Override


Mime
View raw message