You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by ahgittin <gi...@git.apache.org> on 2014/06/24 15:13:07 UTC
[GitHub] incubator-brooklyn pull request: HA use last modified time
GitHub user ahgittin opened a pull request:
https://github.com/apache/incubator-brooklyn/pull/12
HA use last modified time
resubmit of https://github.com/brooklyncentral/brooklyn/pull/1487, adding related code review comments from https://github.com/brooklyncentral/brooklyn/pull/1464
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/ahgittin/incubator-brooklyn ha-use-last-mod-time
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/incubator-brooklyn/pull/12.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #12
----
commit 466f061ec92f7669d9e1fdcc832decc83a8043b8
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Date: 2014-06-17T20:57:16Z
add support for last-modified-date for persistent store objects, and use that to determine which mgmt nodes are live
commit 90f3231c5aaf63809736e794ded1292c0ca6f89f
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Date: 2014-06-24T10:34:23Z
address code review comments from brooklyncentral/brooklyn#1464 - mainly tidying up names and logging
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA uses shared timestamp (last mo...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/12#discussion_r14234616
--- Diff: core/src/test/java/brooklyn/entity/rebind/persister/ListeningObjectStore.java ---
@@ -165,27 +178,54 @@ public ListeningAccessor(String path, StoreObjectAccessor delegate) {
this.path = path;
this.delegate = delegate;
}
+ @Override
public boolean exists() {
return delegate.exists();
}
+ @Override
public void put(String val) {
- listener.recordDataOut("writing "+path, val.length());
+ if (writesFailSilently)
+ return;
+
+ for (ObjectStoreTransactionListener listener: listeners)
+ listener.recordDataOut("writing "+path, val.length());
delegate.put(val);
}
+ @Override
public void append(String s) {
- listener.recordDataOut("appending "+path, s.length());
+ if (writesFailSilently)
+ return;
+
+ for (ObjectStoreTransactionListener listener: listeners)
+ listener.recordDataOut("appending "+path, s.length());
delegate.append(s);
}
+ @Override
public void delete() {
- listener.recordQueryOut("deleting "+path, path.length());
+ if (writesFailSilently)
+ return;
+
+ for (ObjectStoreTransactionListener listener: listeners)
+ listener.recordQueryOut("deleting "+path, path.length());
delegate.delete();
}
+ @Override
public String get() {
- listener.recordQueryOut("requesting "+path, path.length());
+ for (ObjectStoreTransactionListener listener: listeners)
+ listener.recordQueryOut("requesting "+path, path.length());
String result = delegate.get();
- listener.recordDataIn("reading "+path, result.length());
+
+ for (ObjectStoreTransactionListener listener: listeners)
+ listener.recordDataIn("reading "+path, (result==null ? 0 : result.length()));
return result;
}
+ @Override
+ public Date getLastModifiedDate() {
+ return delegate.getLastModifiedDate();
+ }
}
+ public void setWritesFailSilently(boolean writesFailSilently) {
--- End diff --
Ah, it's in test code! Ignore me.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA uses shared timestamp (last mo...
Posted by ahgittin <gi...@git.apache.org>.
Github user ahgittin commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/12#issuecomment-47179394
thanks @aledsage yes, i've just addressed the one comment from your PR. but i added another nifty fix which we discovered was needed in production, so pls look at https://github.com/ahgittin/incubator-brooklyn/commit/f6a4b23bbdb8d6ae17c44bcff9ff7e8f2d98a89a then https://github.com/ahgittin/incubator-brooklyn/commit/36cd96eda1eb3bb42202f0c15fff32280b93ad96 above primarily.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA uses shared timestamp (last mo...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/12#issuecomment-47214549
Looks great - nice fix for split brain.
A few low-priority review comments, but they can be looked at and responded to separately.
Merging now.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA uses shared timestamp (last mo...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/12#discussion_r14235240
--- Diff: core/src/main/java/brooklyn/management/ha/HighAvailabilityManagerImpl.java ---
@@ -437,17 +486,30 @@ protected void promoteToMaster() {
managementContext.getRebindManager().start();
}
+ protected void demoteToStandby() {
+ if (!running) {
+ LOG.warn("Ignoring demote-from-master request, as HighAvailabilityManager is no longer running");
+ return;
+ }
+
+ nodeState = ManagementNodeState.STANDBY;
+ managementContext.getRebindManager().stop();
+ for (Application app: managementContext.getApplications())
+ Entities.unmanage(app);
--- End diff --
Medium term, I'd like to review code for all our entities to ensure that on unmanage they really do stop all their work (and don't have their own thread kicking around or whatever). Also would need to think about what happens to effectors currently executing.
But this is good enough for now.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA use last modified time
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/12#issuecomment-47114576
@ahgittin any comments on the comments in the original https://github.com/brooklyncentral/brooklyn/pull/1487?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA uses shared timestamp (last mo...
Posted by ahgittin <gi...@git.apache.org>.
Github user ahgittin commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/12#discussion_r14235671
--- Diff: core/src/main/java/brooklyn/management/ha/HighAvailabilityManagerImpl.java ---
@@ -384,24 +418,39 @@ protected void checkMaster(boolean initializing) {
return;
}
+ if (demotingSelfInFavourOfOtherMaster) {
+ LOG.debug("Master-change for this node only, demoting "+ownNodeMemento.toVerboseString()+" in favour of official master "+newMasterRecord);
+ demoteToStandby();
+ return;
+ }
+
// Need to choose a new master
- ManagementNodeSyncRecord newMasterRecord = masterChooser.choose(memento, heartbeatTimeout, ownNodeId, now);
+ newMasterRecord = masterChooser.choose(memento, heartbeatTimeout, ownNodeId);
+
String newMasterNodeId = (newMasterRecord == null) ? null : newMasterRecord.getNodeId();
URI newMasterNodeUri = (newMasterRecord == null) ? null : newMasterRecord.getUri();
boolean newMasterIsSelf = ownNodeId.equals(newMasterNodeId);
- LOG.debug("Management node master-promotion required: newMaster={}; oldMaster={}; plane={}, self={}; heartbeatTimeout={}",
- new Object[] {
- (newMasterRecord == null ? "<none>" : newMasterRecord.toVerboseString()),
- (masterNodeMemento == null ? masterNodeId+" (no memento)": masterNodeMemento.toVerboseString()),
- memento,
- ownNodeMemento.toVerboseString(),
- heartbeatTimeout
- });
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Management node master-change required: newMaster={}; oldMaster={}; plane={}, self={}; heartbeatTimeout={}",
+ new Object[] {
+ (newMasterRecord == null ? "<none>" : newMasterRecord.toVerboseString()),
+ (masterNodeMemento == null ? masterNodeId+" (no memento)": masterNodeMemento.toVerboseString()),
+ memento,
+ ownNodeMemento.toVerboseString(),
+ heartbeatTimeout
+ });
+ }
if (!initializing) {
- LOG.warn("HA subsystem detected change of master from "+masterNodeId+" to "
- + (newMasterNodeId == null ? "<unknown>" :
- newMasterNodeId + (newMasterNodeUri!=null ? " "+newMasterNodeUri : "")));
+ if (!demotingSelfInFavourOfOtherMaster) {
--- End diff --
yep, will fix
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA uses shared timestamp (last mo...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/12#discussion_r14234461
--- Diff: core/src/test/java/brooklyn/entity/rebind/persister/ListeningObjectStore.java ---
@@ -101,9 +104,10 @@ public void blockUntilDataWrittenExceeds(long count, Duration timeout) throws In
}
}
- public ListeningObjectStore(PersistenceObjectStore delegate, ObjectStoreTransactionListener listener) {
+ public ListeningObjectStore(PersistenceObjectStore delegate, ObjectStoreTransactionListener ...listeners) {
--- End diff --
Extremely minor: personal preference for the `...` to be next to the class name rather than the variable name. It is part of type of the parameter, rather than part of the name.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA uses shared timestamp (last mo...
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/incubator-brooklyn/pull/12
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA uses shared timestamp (last mo...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/12#discussion_r14234589
--- Diff: core/src/test/java/brooklyn/entity/rebind/persister/ListeningObjectStore.java ---
@@ -165,27 +178,54 @@ public ListeningAccessor(String path, StoreObjectAccessor delegate) {
this.path = path;
this.delegate = delegate;
}
+ @Override
public boolean exists() {
return delegate.exists();
}
+ @Override
public void put(String val) {
- listener.recordDataOut("writing "+path, val.length());
+ if (writesFailSilently)
+ return;
+
+ for (ObjectStoreTransactionListener listener: listeners)
+ listener.recordDataOut("writing "+path, val.length());
delegate.put(val);
}
+ @Override
public void append(String s) {
- listener.recordDataOut("appending "+path, s.length());
+ if (writesFailSilently)
+ return;
+
+ for (ObjectStoreTransactionListener listener: listeners)
+ listener.recordDataOut("appending "+path, s.length());
delegate.append(s);
}
+ @Override
public void delete() {
- listener.recordQueryOut("deleting "+path, path.length());
+ if (writesFailSilently)
+ return;
+
+ for (ObjectStoreTransactionListener listener: listeners)
+ listener.recordQueryOut("deleting "+path, path.length());
delegate.delete();
}
+ @Override
public String get() {
- listener.recordQueryOut("requesting "+path, path.length());
+ for (ObjectStoreTransactionListener listener: listeners)
+ listener.recordQueryOut("requesting "+path, path.length());
String result = delegate.get();
- listener.recordDataIn("reading "+path, result.length());
+
+ for (ObjectStoreTransactionListener listener: listeners)
+ listener.recordDataIn("reading "+path, (result==null ? 0 : result.length()));
return result;
}
+ @Override
+ public Date getLastModifiedDate() {
+ return delegate.getLastModifiedDate();
+ }
}
+ public void setWritesFailSilently(boolean writesFailSilently) {
--- End diff --
Presumably `@VisibleForTesting` - looks like it would be really bad to call this in production, as it calls all writes to be ignored rather than just ignoring exceptions on writes. Worth javadoc to that effect.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA uses shared timestamp (last mo...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/12#discussion_r14234981
--- Diff: core/src/test/java/brooklyn/management/ha/HighAvailabilityManagerSplitBrainTest.java ---
@@ -0,0 +1,311 @@
+package brooklyn.management.ha;
+
+import static org.testng.Assert.assertEquals;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import brooklyn.entity.basic.ApplicationBuilder;
+import brooklyn.entity.basic.Entities;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.entity.rebind.persister.BrooklynMementoPersisterToObjectStore;
+import brooklyn.entity.rebind.persister.InMemoryObjectStore;
+import brooklyn.entity.rebind.persister.ListeningObjectStore;
+import brooklyn.entity.rebind.persister.PersistMode;
+import brooklyn.entity.rebind.persister.PersistenceObjectStore;
+import brooklyn.location.Location;
+import brooklyn.management.internal.ManagementContextInternal;
+import brooklyn.test.entity.LocalManagementContextForTests;
+import brooklyn.test.entity.TestApplication;
+import brooklyn.util.collections.MutableList;
+import brooklyn.util.collections.MutableMap;
+import brooklyn.util.repeat.Repeater;
+import brooklyn.util.time.Duration;
+import brooklyn.util.time.Time;
+
+import com.google.common.base.Ticker;
+import com.google.common.collect.ImmutableList;
+
+@Test
+public class HighAvailabilityManagerSplitBrainTest {
+
+ private static final Logger log = LoggerFactory.getLogger(HighAvailabilityManagerSplitBrainTest.class);
+
+ private List<HaMgmtNode> nodes = new MutableList<HighAvailabilityManagerSplitBrainTest.HaMgmtNode>();
+ Map<String,String> sharedBackingStore = MutableMap.of();
+ Map<String,Date> sharedBackingStoreDates = MutableMap.of();
+ private AtomicLong sharedTime; // used to set the ticker's return value
+ private ClassLoader classLoader = getClass().getClassLoader();
+
+ public class HaMgmtNode {
+
+ private ManagementContextInternal mgmt;
+ private String ownNodeId;
+ private String nodeName;
+ private ListeningObjectStore objectStore;
+ private ManagementPlaneSyncRecordPersister persister;
+ private HighAvailabilityManagerImpl ha;
+ private Ticker ticker;
+ private AtomicLong currentTime; // used to set the ticker's return value
+
+ @BeforeMethod(alwaysRun=true)
+ public void setUp() throws Exception {
+ if (sharedTime==null)
+ currentTime = new AtomicLong(System.currentTimeMillis());
+
+ ticker = new Ticker() {
+ // strictly not a ticker because returns millis UTC, but it works fine even so
+ @Override public long read() {
+ if (sharedTime!=null) return sharedTime.get();
+ return currentTime.get();
+ }
+ };
+
+ nodeName = "node "+nodes.size();
+ mgmt = newLocalManagementContext();
+ ownNodeId = mgmt.getManagementNodeId();
+ objectStore = new ListeningObjectStore(newPersistenceObjectStore());
+ objectStore.injectManagementContext(mgmt);
+ objectStore.prepareForSharedUse(PersistMode.CLEAN, HighAvailabilityMode.DISABLED);
+ persister = new ManagementPlaneSyncRecordPersisterToObjectStore(mgmt, objectStore, classLoader);
+ ((ManagementPlaneSyncRecordPersisterToObjectStore)persister).allowRemoteTimestampInMemento();
+ BrooklynMementoPersisterToObjectStore persisterObj = new BrooklynMementoPersisterToObjectStore(objectStore, classLoader);
+ mgmt.getRebindManager().setPersister(persisterObj);
+ ha = new HighAvailabilityManagerImpl(mgmt)
+ .setPollPeriod(Duration.PRACTICALLY_FOREVER)
+ .setHeartbeatTimeout(Duration.THIRTY_SECONDS)
+ .setLocalTicker(ticker)
+ .setRemoteTicker(ticker)
+ .setPersister(persister);
+ log.info("Created "+nodeName+" "+ownNodeId);
+ }
+
+ public void tearDown() throws Exception {
+ if (ha != null) ha.stop();
+ if (mgmt != null) Entities.destroyAll(mgmt);
+ if (objectStore != null) objectStore.deleteCompletely();
+ }
+
+ private long tickerCurrentMillis() {
+ return ticker.read();
+ }
+
+ private long tickerAdvance(Duration duration) {
+ if (sharedTime!=null)
+ throw new IllegalStateException("Using shared ticker; cannot advance private node clock");
+ currentTime.addAndGet(duration.toMilliseconds());
+ return tickerCurrentMillis();
+ }
+
+ @Override
+ public String toString() {
+ return nodeName+" "+ownNodeId;
+ }
+ }
+
+ @BeforeMethod(alwaysRun=true)
+ public void setUp() throws Exception {
+ nodes.clear();
+ sharedBackingStore.clear();
+ }
+
+ public HaMgmtNode newNode() throws Exception {
+ HaMgmtNode node = new HaMgmtNode();
+ node.setUp();
+ nodes.add(node);
+ return node;
+ }
+
+ @AfterMethod(alwaysRun=true)
+ public void tearDown() throws Exception {
+ for (HaMgmtNode n: nodes)
+ n.tearDown();
+ }
+
+ private void sharedTickerAdvance(Duration duration) {
+ if (sharedTime==null) {
+ for (HaMgmtNode n: nodes)
+ n.tickerAdvance(duration);
+ } else {
+ sharedTime.addAndGet(duration.toMilliseconds());
+ }
+ }
+
+ private long sharedTickerCurrentMillis() {
+ return sharedTime.get();
+ }
+
+ protected void useSharedTime() {
+ if (!nodes.isEmpty())
+ throw new IllegalStateException("shared time must be set up before any nodes created");
+ sharedTime = new AtomicLong(System.currentTimeMillis());
+ }
+
+ protected ManagementContextInternal newLocalManagementContext() {
+ return new LocalManagementContextForTests();
+ }
+
+ protected PersistenceObjectStore newPersistenceObjectStore() {
+ return new InMemoryObjectStore(sharedBackingStore, sharedBackingStoreDates);
+ }
+
+ @Test
+ public void testIfNodeStopsBeingAbleToWrite() throws Exception {
+ useSharedTime();
+ HaMgmtNode n1 = newNode();
+ HaMgmtNode n2 = newNode();
+
+ n1.ha.start(HighAvailabilityMode.AUTO);
+ ManagementPlaneSyncRecord memento1 = n1.ha.getManagementPlaneSyncState();
+
+ log.info(n1+" HA: "+memento1);
+ assertEquals(memento1.getMasterNodeId(), n1.ownNodeId);
+ Long time0 = sharedTickerCurrentMillis();
+ assertEquals(memento1.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time0);
+ assertEquals(memento1.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+
+ n2.ha.start(HighAvailabilityMode.AUTO);
+ ManagementPlaneSyncRecord memento2 = n2.ha.getManagementPlaneSyncState();
+
+ log.info(n2+" HA: "+memento2);
+ assertEquals(memento2.getMasterNodeId(), n1.ownNodeId);
+ assertEquals(memento2.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+ assertEquals(memento2.getManagementNodes().get(n2.ownNodeId).getStatus(), ManagementNodeState.STANDBY);
+ assertEquals(memento2.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time0);
+ assertEquals(memento2.getManagementNodes().get(n2.ownNodeId).getRemoteTimestamp(), time0);
+
+ // and no entities at either
+ assertEquals(n1.mgmt.getApplications().size(), 0);
+ assertEquals(n2.mgmt.getApplications().size(), 0);
+
+ // create
+ TestApplication app = ApplicationBuilder.newManagedApp(EntitySpec.create(TestApplication.class), n1.mgmt);
+ app.start(ImmutableList.<Location>of());
+ app.setAttribute(TestApplication.MY_ATTRIBUTE, "hello");
+
+ assertEquals(n1.mgmt.getApplications().size(), 1);
+ assertEquals(n2.mgmt.getApplications().size(), 0);
+ n1.mgmt.getRebindManager().forcePersistNow();
+
+ n1.objectStore.setWritesFailSilently(true);
+ log.info(n1+" writes off");
+ sharedTickerAdvance(Duration.ONE_MINUTE);
+
+ n2.ha.publishAndCheck(false);
+ ManagementPlaneSyncRecord memento2b = n2.ha.getManagementPlaneSyncState();
+ log.info(n2+" HA now: "+memento2b);
+ Long time1 = sharedTickerCurrentMillis();
+
+ // n2 infers n1 as failed
+ assertEquals(memento2b.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.FAILED);
+ assertEquals(memento2b.getManagementNodes().get(n2.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+ assertEquals(memento2b.getMasterNodeId(), n2.ownNodeId);
+ assertEquals(memento2b.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time0);
+ assertEquals(memento2b.getManagementNodes().get(n2.ownNodeId).getRemoteTimestamp(), time1);
+
+ assertEquals(n1.mgmt.getApplications().size(), 1);
+ assertEquals(n2.mgmt.getApplications().size(), 1);
+ assertEquals(n1.mgmt.getApplications().iterator().next().getAttribute(TestApplication.MY_ATTRIBUTE), "hello");
+
+ n1.objectStore.setWritesFailSilently(false);
+ log.info(n1+" writes on");
+
+ sharedTickerAdvance(Duration.ONE_SECOND);
+ Long time2 = sharedTickerCurrentMillis();
+
+ n1.ha.publishAndCheck(false);
+ ManagementPlaneSyncRecord memento1b = n1.ha.getManagementPlaneSyncState();
+ log.info(n1+" HA now: "+memento1b);
+
+ // n1 comes back and demotes himself
+ assertEquals(memento1b.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.STANDBY);
+ assertEquals(memento1b.getManagementNodes().get(n2.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+ assertEquals(memento1b.getMasterNodeId(), n2.ownNodeId);
+ assertEquals(memento1b.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time2);
+ assertEquals(memento1b.getManagementNodes().get(n2.ownNodeId).getRemoteTimestamp(), time1);
+
+ // n2 now sees itself as master, with n1 in standby again
+ ManagementPlaneSyncRecord memento2c = n2.ha.getManagementPlaneSyncState();
+ log.info(n2+" HA now: "+memento2c);
+ assertEquals(memento2c.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.STANDBY);
+ assertEquals(memento2c.getManagementNodes().get(n2.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+ assertEquals(memento2c.getMasterNodeId(), n2.ownNodeId);
+ assertEquals(memento2c.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time2);
+ assertEquals(memento2c.getManagementNodes().get(n2.ownNodeId).getRemoteTimestamp(), time2);
+
+ // and no entities at n1
+ assertEquals(n1.mgmt.getApplications().size(), 0);
+ assertEquals(n2.mgmt.getApplications().size(), 1);
+ }
+
+ @Test(invocationCount=50)
+ public void testIfNodeStopsBeingAbleToWriteManyTimes() throws Exception {
+ testIfNodeStopsBeingAbleToWrite();
+ }
+
+ @Test
+ public void testSimultaneousStartup() throws Exception {
+ doTestConcurrentStartup(5, null);
+ }
+
+ @Test
+ public void testNearSimultaneousStartup() throws Exception {
+ doTestConcurrentStartup(20, Duration.millis(20));
+ }
+
+ @Test(invocationCount=50, groups="Integration")
+ public void testNearSimultaneousStartupManyTimes() throws Exception {
+ doTestConcurrentStartup(20, Duration.millis(20));
+ }
+
+ protected void doTestConcurrentStartup(int size, final Duration staggerStart) throws Exception {
+ useSharedTime();
+
+ List<Thread> spawned = MutableList.of();
+ for (int i=0; i<size; i++) {
+ final HaMgmtNode n = newNode();
+ Thread t = new Thread() { public void run() {
+ if (staggerStart!=null) Time.sleep(staggerStart.multiply(Math.random()));
+ n.ha.start(HighAvailabilityMode.AUTO);
+ } };
+ spawned.add(t);
+ t.start();
+ }
+
+ Assert.assertTrue(Repeater.create().every(Duration.millis(1)).limitTimeTo(Duration.TEN_SECONDS).until(new Callable<Boolean>() {
+ @Override public Boolean call() throws Exception {
+ ManagementPlaneSyncRecord memento = nodes.get(0).ha.getManagementPlaneSyncState();
+ int masters=0, standbys=0, savedMasters=0, savedStandbys=0;
+ for (HaMgmtNode n: nodes) {
+ if (n.ha.getNodeState()==ManagementNodeState.MASTER) masters++;
+ if (n.ha.getNodeState()==ManagementNodeState.STANDBY) standbys++;
+ ManagementNodeSyncRecord m = memento.getManagementNodes().get(n.ownNodeId);
+ if (m!=null) {
+ if (m.getStatus()==ManagementNodeState.MASTER) savedMasters++;
+ if (m.getStatus()==ManagementNodeState.STANDBY) savedStandbys++;
+ }
+ }
+ log.info("starting "+nodes.size()+" nodes: "+masters+" M + "+standbys+" zzz; "
+ + memento.getManagementNodes().size()+" saved, "
+ + memento.getMasterNodeId()+" master, "+savedMasters+" M + "+savedStandbys+" zzz");
+
+ return masters==1 && standbys==nodes.size()-1 && savedMasters==1 && savedStandbys==nodes.size()-1;
+ }
+ }).run());
+
+ for (Thread t: spawned)
+ t.join(Duration.FIVE_SECONDS.toMilliseconds());
--- End diff --
I prefer long timeouts for anything that cloudbees will run - e.g. thirty seconds.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA uses shared timestamp (last mo...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/12#discussion_r14235324
--- Diff: core/src/test/java/brooklyn/management/ha/HighAvailabilityManagerSplitBrainTest.java ---
@@ -0,0 +1,311 @@
+package brooklyn.management.ha;
+
+import static org.testng.Assert.assertEquals;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import brooklyn.entity.basic.ApplicationBuilder;
+import brooklyn.entity.basic.Entities;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.entity.rebind.persister.BrooklynMementoPersisterToObjectStore;
+import brooklyn.entity.rebind.persister.InMemoryObjectStore;
+import brooklyn.entity.rebind.persister.ListeningObjectStore;
+import brooklyn.entity.rebind.persister.PersistMode;
+import brooklyn.entity.rebind.persister.PersistenceObjectStore;
+import brooklyn.location.Location;
+import brooklyn.management.internal.ManagementContextInternal;
+import brooklyn.test.entity.LocalManagementContextForTests;
+import brooklyn.test.entity.TestApplication;
+import brooklyn.util.collections.MutableList;
+import brooklyn.util.collections.MutableMap;
+import brooklyn.util.repeat.Repeater;
+import brooklyn.util.time.Duration;
+import brooklyn.util.time.Time;
+
+import com.google.common.base.Ticker;
+import com.google.common.collect.ImmutableList;
+
+@Test
+public class HighAvailabilityManagerSplitBrainTest {
+
+ private static final Logger log = LoggerFactory.getLogger(HighAvailabilityManagerSplitBrainTest.class);
+
+ private List<HaMgmtNode> nodes = new MutableList<HighAvailabilityManagerSplitBrainTest.HaMgmtNode>();
+ Map<String,String> sharedBackingStore = MutableMap.of();
+ Map<String,Date> sharedBackingStoreDates = MutableMap.of();
+ private AtomicLong sharedTime; // used to set the ticker's return value
+ private ClassLoader classLoader = getClass().getClassLoader();
+
+ public class HaMgmtNode {
+
+ private ManagementContextInternal mgmt;
+ private String ownNodeId;
+ private String nodeName;
+ private ListeningObjectStore objectStore;
+ private ManagementPlaneSyncRecordPersister persister;
+ private HighAvailabilityManagerImpl ha;
+ private Ticker ticker;
+ private AtomicLong currentTime; // used to set the ticker's return value
+
+ @BeforeMethod(alwaysRun=true)
+ public void setUp() throws Exception {
+ if (sharedTime==null)
+ currentTime = new AtomicLong(System.currentTimeMillis());
+
+ ticker = new Ticker() {
+ // strictly not a ticker because returns millis UTC, but it works fine even so
+ @Override public long read() {
+ if (sharedTime!=null) return sharedTime.get();
+ return currentTime.get();
+ }
+ };
+
+ nodeName = "node "+nodes.size();
+ mgmt = newLocalManagementContext();
+ ownNodeId = mgmt.getManagementNodeId();
+ objectStore = new ListeningObjectStore(newPersistenceObjectStore());
+ objectStore.injectManagementContext(mgmt);
+ objectStore.prepareForSharedUse(PersistMode.CLEAN, HighAvailabilityMode.DISABLED);
+ persister = new ManagementPlaneSyncRecordPersisterToObjectStore(mgmt, objectStore, classLoader);
+ ((ManagementPlaneSyncRecordPersisterToObjectStore)persister).allowRemoteTimestampInMemento();
+ BrooklynMementoPersisterToObjectStore persisterObj = new BrooklynMementoPersisterToObjectStore(objectStore, classLoader);
+ mgmt.getRebindManager().setPersister(persisterObj);
+ ha = new HighAvailabilityManagerImpl(mgmt)
+ .setPollPeriod(Duration.PRACTICALLY_FOREVER)
+ .setHeartbeatTimeout(Duration.THIRTY_SECONDS)
+ .setLocalTicker(ticker)
+ .setRemoteTicker(ticker)
+ .setPersister(persister);
+ log.info("Created "+nodeName+" "+ownNodeId);
+ }
+
+ public void tearDown() throws Exception {
+ if (ha != null) ha.stop();
+ if (mgmt != null) Entities.destroyAll(mgmt);
+ if (objectStore != null) objectStore.deleteCompletely();
+ }
+
+ private long tickerCurrentMillis() {
+ return ticker.read();
+ }
+
+ private long tickerAdvance(Duration duration) {
+ if (sharedTime!=null)
+ throw new IllegalStateException("Using shared ticker; cannot advance private node clock");
+ currentTime.addAndGet(duration.toMilliseconds());
+ return tickerCurrentMillis();
+ }
+
+ @Override
+ public String toString() {
+ return nodeName+" "+ownNodeId;
+ }
+ }
+
+ @BeforeMethod(alwaysRun=true)
+ public void setUp() throws Exception {
+ nodes.clear();
+ sharedBackingStore.clear();
+ }
+
+ public HaMgmtNode newNode() throws Exception {
+ HaMgmtNode node = new HaMgmtNode();
+ node.setUp();
+ nodes.add(node);
+ return node;
+ }
+
+ @AfterMethod(alwaysRun=true)
+ public void tearDown() throws Exception {
+ for (HaMgmtNode n: nodes)
+ n.tearDown();
+ }
+
+ private void sharedTickerAdvance(Duration duration) {
+ if (sharedTime==null) {
+ for (HaMgmtNode n: nodes)
+ n.tickerAdvance(duration);
+ } else {
+ sharedTime.addAndGet(duration.toMilliseconds());
+ }
+ }
+
+ private long sharedTickerCurrentMillis() {
+ return sharedTime.get();
+ }
+
+ protected void useSharedTime() {
+ if (!nodes.isEmpty())
+ throw new IllegalStateException("shared time must be set up before any nodes created");
+ sharedTime = new AtomicLong(System.currentTimeMillis());
+ }
+
+ protected ManagementContextInternal newLocalManagementContext() {
+ return new LocalManagementContextForTests();
+ }
+
+ protected PersistenceObjectStore newPersistenceObjectStore() {
+ return new InMemoryObjectStore(sharedBackingStore, sharedBackingStoreDates);
+ }
+
+ @Test
+ public void testIfNodeStopsBeingAbleToWrite() throws Exception {
+ useSharedTime();
+ HaMgmtNode n1 = newNode();
+ HaMgmtNode n2 = newNode();
+
+ n1.ha.start(HighAvailabilityMode.AUTO);
+ ManagementPlaneSyncRecord memento1 = n1.ha.getManagementPlaneSyncState();
+
+ log.info(n1+" HA: "+memento1);
+ assertEquals(memento1.getMasterNodeId(), n1.ownNodeId);
+ Long time0 = sharedTickerCurrentMillis();
+ assertEquals(memento1.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time0);
+ assertEquals(memento1.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+
+ n2.ha.start(HighAvailabilityMode.AUTO);
+ ManagementPlaneSyncRecord memento2 = n2.ha.getManagementPlaneSyncState();
+
+ log.info(n2+" HA: "+memento2);
+ assertEquals(memento2.getMasterNodeId(), n1.ownNodeId);
+ assertEquals(memento2.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+ assertEquals(memento2.getManagementNodes().get(n2.ownNodeId).getStatus(), ManagementNodeState.STANDBY);
+ assertEquals(memento2.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time0);
+ assertEquals(memento2.getManagementNodes().get(n2.ownNodeId).getRemoteTimestamp(), time0);
+
+ // and no entities at either
+ assertEquals(n1.mgmt.getApplications().size(), 0);
+ assertEquals(n2.mgmt.getApplications().size(), 0);
+
+ // create
+ TestApplication app = ApplicationBuilder.newManagedApp(EntitySpec.create(TestApplication.class), n1.mgmt);
+ app.start(ImmutableList.<Location>of());
+ app.setAttribute(TestApplication.MY_ATTRIBUTE, "hello");
+
+ assertEquals(n1.mgmt.getApplications().size(), 1);
+ assertEquals(n2.mgmt.getApplications().size(), 0);
+ n1.mgmt.getRebindManager().forcePersistNow();
--- End diff --
I'm fine with this, but note for other places...
The more complicated `waitForWritesComplete` call ensures that tests don't only pass because we're explicitly calling `persistNow`. Instead other tests are more passively observing what the production persistence code is doing. So let's not change the other uses of `waitForWritesComplete`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA uses shared timestamp (last mo...
Posted by ahgittin <gi...@git.apache.org>.
Github user ahgittin commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/12#discussion_r14235738
--- Diff: core/src/test/java/brooklyn/management/ha/HighAvailabilityManagerSplitBrainTest.java ---
@@ -0,0 +1,311 @@
+package brooklyn.management.ha;
+
+import static org.testng.Assert.assertEquals;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import brooklyn.entity.basic.ApplicationBuilder;
+import brooklyn.entity.basic.Entities;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.entity.rebind.persister.BrooklynMementoPersisterToObjectStore;
+import brooklyn.entity.rebind.persister.InMemoryObjectStore;
+import brooklyn.entity.rebind.persister.ListeningObjectStore;
+import brooklyn.entity.rebind.persister.PersistMode;
+import brooklyn.entity.rebind.persister.PersistenceObjectStore;
+import brooklyn.location.Location;
+import brooklyn.management.internal.ManagementContextInternal;
+import brooklyn.test.entity.LocalManagementContextForTests;
+import brooklyn.test.entity.TestApplication;
+import brooklyn.util.collections.MutableList;
+import brooklyn.util.collections.MutableMap;
+import brooklyn.util.repeat.Repeater;
+import brooklyn.util.time.Duration;
+import brooklyn.util.time.Time;
+
+import com.google.common.base.Ticker;
+import com.google.common.collect.ImmutableList;
+
+@Test
+public class HighAvailabilityManagerSplitBrainTest {
+
+ private static final Logger log = LoggerFactory.getLogger(HighAvailabilityManagerSplitBrainTest.class);
+
+ private List<HaMgmtNode> nodes = new MutableList<HighAvailabilityManagerSplitBrainTest.HaMgmtNode>();
+ Map<String,String> sharedBackingStore = MutableMap.of();
+ Map<String,Date> sharedBackingStoreDates = MutableMap.of();
+ private AtomicLong sharedTime; // used to set the ticker's return value
+ private ClassLoader classLoader = getClass().getClassLoader();
+
+ public class HaMgmtNode {
+
+ private ManagementContextInternal mgmt;
+ private String ownNodeId;
+ private String nodeName;
+ private ListeningObjectStore objectStore;
+ private ManagementPlaneSyncRecordPersister persister;
+ private HighAvailabilityManagerImpl ha;
+ private Ticker ticker;
+ private AtomicLong currentTime; // used to set the ticker's return value
+
+ @BeforeMethod(alwaysRun=true)
+ public void setUp() throws Exception {
+ if (sharedTime==null)
+ currentTime = new AtomicLong(System.currentTimeMillis());
+
+ ticker = new Ticker() {
+ // strictly not a ticker because returns millis UTC, but it works fine even so
+ @Override public long read() {
+ if (sharedTime!=null) return sharedTime.get();
+ return currentTime.get();
+ }
+ };
+
+ nodeName = "node "+nodes.size();
+ mgmt = newLocalManagementContext();
+ ownNodeId = mgmt.getManagementNodeId();
+ objectStore = new ListeningObjectStore(newPersistenceObjectStore());
+ objectStore.injectManagementContext(mgmt);
+ objectStore.prepareForSharedUse(PersistMode.CLEAN, HighAvailabilityMode.DISABLED);
+ persister = new ManagementPlaneSyncRecordPersisterToObjectStore(mgmt, objectStore, classLoader);
+ ((ManagementPlaneSyncRecordPersisterToObjectStore)persister).allowRemoteTimestampInMemento();
+ BrooklynMementoPersisterToObjectStore persisterObj = new BrooklynMementoPersisterToObjectStore(objectStore, classLoader);
+ mgmt.getRebindManager().setPersister(persisterObj);
+ ha = new HighAvailabilityManagerImpl(mgmt)
+ .setPollPeriod(Duration.PRACTICALLY_FOREVER)
+ .setHeartbeatTimeout(Duration.THIRTY_SECONDS)
+ .setLocalTicker(ticker)
+ .setRemoteTicker(ticker)
+ .setPersister(persister);
+ log.info("Created "+nodeName+" "+ownNodeId);
+ }
+
+ public void tearDown() throws Exception {
+ if (ha != null) ha.stop();
+ if (mgmt != null) Entities.destroyAll(mgmt);
+ if (objectStore != null) objectStore.deleteCompletely();
+ }
+
+ private long tickerCurrentMillis() {
+ return ticker.read();
+ }
+
+ private long tickerAdvance(Duration duration) {
+ if (sharedTime!=null)
+ throw new IllegalStateException("Using shared ticker; cannot advance private node clock");
+ currentTime.addAndGet(duration.toMilliseconds());
+ return tickerCurrentMillis();
+ }
+
+ @Override
+ public String toString() {
+ return nodeName+" "+ownNodeId;
+ }
+ }
+
+ @BeforeMethod(alwaysRun=true)
+ public void setUp() throws Exception {
+ nodes.clear();
+ sharedBackingStore.clear();
+ }
+
+ public HaMgmtNode newNode() throws Exception {
+ HaMgmtNode node = new HaMgmtNode();
+ node.setUp();
+ nodes.add(node);
+ return node;
+ }
+
+ @AfterMethod(alwaysRun=true)
+ public void tearDown() throws Exception {
+ for (HaMgmtNode n: nodes)
+ n.tearDown();
+ }
+
+ private void sharedTickerAdvance(Duration duration) {
+ if (sharedTime==null) {
+ for (HaMgmtNode n: nodes)
+ n.tickerAdvance(duration);
+ } else {
+ sharedTime.addAndGet(duration.toMilliseconds());
+ }
+ }
+
+ private long sharedTickerCurrentMillis() {
+ return sharedTime.get();
+ }
+
+ protected void useSharedTime() {
+ if (!nodes.isEmpty())
+ throw new IllegalStateException("shared time must be set up before any nodes created");
+ sharedTime = new AtomicLong(System.currentTimeMillis());
+ }
+
+ protected ManagementContextInternal newLocalManagementContext() {
+ return new LocalManagementContextForTests();
+ }
+
+ protected PersistenceObjectStore newPersistenceObjectStore() {
+ return new InMemoryObjectStore(sharedBackingStore, sharedBackingStoreDates);
+ }
+
+ @Test
+ public void testIfNodeStopsBeingAbleToWrite() throws Exception {
+ useSharedTime();
+ HaMgmtNode n1 = newNode();
+ HaMgmtNode n2 = newNode();
+
+ n1.ha.start(HighAvailabilityMode.AUTO);
+ ManagementPlaneSyncRecord memento1 = n1.ha.getManagementPlaneSyncState();
+
+ log.info(n1+" HA: "+memento1);
+ assertEquals(memento1.getMasterNodeId(), n1.ownNodeId);
+ Long time0 = sharedTickerCurrentMillis();
+ assertEquals(memento1.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time0);
+ assertEquals(memento1.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+
+ n2.ha.start(HighAvailabilityMode.AUTO);
+ ManagementPlaneSyncRecord memento2 = n2.ha.getManagementPlaneSyncState();
+
+ log.info(n2+" HA: "+memento2);
+ assertEquals(memento2.getMasterNodeId(), n1.ownNodeId);
+ assertEquals(memento2.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+ assertEquals(memento2.getManagementNodes().get(n2.ownNodeId).getStatus(), ManagementNodeState.STANDBY);
+ assertEquals(memento2.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time0);
+ assertEquals(memento2.getManagementNodes().get(n2.ownNodeId).getRemoteTimestamp(), time0);
+
+ // and no entities at either
+ assertEquals(n1.mgmt.getApplications().size(), 0);
+ assertEquals(n2.mgmt.getApplications().size(), 0);
+
+ // create
+ TestApplication app = ApplicationBuilder.newManagedApp(EntitySpec.create(TestApplication.class), n1.mgmt);
+ app.start(ImmutableList.<Location>of());
+ app.setAttribute(TestApplication.MY_ATTRIBUTE, "hello");
+
+ assertEquals(n1.mgmt.getApplications().size(), 1);
+ assertEquals(n2.mgmt.getApplications().size(), 0);
+ n1.mgmt.getRebindManager().forcePersistNow();
+
+ n1.objectStore.setWritesFailSilently(true);
+ log.info(n1+" writes off");
+ sharedTickerAdvance(Duration.ONE_MINUTE);
+
+ n2.ha.publishAndCheck(false);
+ ManagementPlaneSyncRecord memento2b = n2.ha.getManagementPlaneSyncState();
+ log.info(n2+" HA now: "+memento2b);
+ Long time1 = sharedTickerCurrentMillis();
+
+ // n2 infers n1 as failed
+ assertEquals(memento2b.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.FAILED);
+ assertEquals(memento2b.getManagementNodes().get(n2.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+ assertEquals(memento2b.getMasterNodeId(), n2.ownNodeId);
+ assertEquals(memento2b.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time0);
+ assertEquals(memento2b.getManagementNodes().get(n2.ownNodeId).getRemoteTimestamp(), time1);
+
+ assertEquals(n1.mgmt.getApplications().size(), 1);
+ assertEquals(n2.mgmt.getApplications().size(), 1);
+ assertEquals(n1.mgmt.getApplications().iterator().next().getAttribute(TestApplication.MY_ATTRIBUTE), "hello");
+
+ n1.objectStore.setWritesFailSilently(false);
+ log.info(n1+" writes on");
+
+ sharedTickerAdvance(Duration.ONE_SECOND);
+ Long time2 = sharedTickerCurrentMillis();
+
+ n1.ha.publishAndCheck(false);
+ ManagementPlaneSyncRecord memento1b = n1.ha.getManagementPlaneSyncState();
+ log.info(n1+" HA now: "+memento1b);
+
+ // n1 comes back and demotes himself
+ assertEquals(memento1b.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.STANDBY);
+ assertEquals(memento1b.getManagementNodes().get(n2.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+ assertEquals(memento1b.getMasterNodeId(), n2.ownNodeId);
+ assertEquals(memento1b.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time2);
+ assertEquals(memento1b.getManagementNodes().get(n2.ownNodeId).getRemoteTimestamp(), time1);
+
+ // n2 now sees itself as master, with n1 in standby again
+ ManagementPlaneSyncRecord memento2c = n2.ha.getManagementPlaneSyncState();
+ log.info(n2+" HA now: "+memento2c);
+ assertEquals(memento2c.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.STANDBY);
+ assertEquals(memento2c.getManagementNodes().get(n2.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+ assertEquals(memento2c.getMasterNodeId(), n2.ownNodeId);
+ assertEquals(memento2c.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time2);
+ assertEquals(memento2c.getManagementNodes().get(n2.ownNodeId).getRemoteTimestamp(), time2);
+
+ // and no entities at n1
+ assertEquals(n1.mgmt.getApplications().size(), 0);
+ assertEquals(n2.mgmt.getApplications().size(), 1);
+ }
+
+ @Test(invocationCount=50)
+ public void testIfNodeStopsBeingAbleToWriteManyTimes() throws Exception {
+ testIfNodeStopsBeingAbleToWrite();
+ }
+
+ @Test
+ public void testSimultaneousStartup() throws Exception {
+ doTestConcurrentStartup(5, null);
+ }
+
+ @Test
+ public void testNearSimultaneousStartup() throws Exception {
+ doTestConcurrentStartup(20, Duration.millis(20));
+ }
+
+ @Test(invocationCount=50, groups="Integration")
+ public void testNearSimultaneousStartupManyTimes() throws Exception {
+ doTestConcurrentStartup(20, Duration.millis(20));
+ }
+
+ protected void doTestConcurrentStartup(int size, final Duration staggerStart) throws Exception {
+ useSharedTime();
+
+ List<Thread> spawned = MutableList.of();
+ for (int i=0; i<size; i++) {
+ final HaMgmtNode n = newNode();
+ Thread t = new Thread() { public void run() {
+ if (staggerStart!=null) Time.sleep(staggerStart.multiply(Math.random()));
+ n.ha.start(HighAvailabilityMode.AUTO);
+ } };
+ spawned.add(t);
+ t.start();
+ }
+
+ Assert.assertTrue(Repeater.create().every(Duration.millis(1)).limitTimeTo(Duration.TEN_SECONDS).until(new Callable<Boolean>() {
--- End diff --
i want the frequent log messages, to see that sometimes it gets 2 or 3 M nodes then settles down at 1
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA uses shared timestamp (last mo...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/12#discussion_r14234952
--- Diff: core/src/test/java/brooklyn/management/ha/HighAvailabilityManagerSplitBrainTest.java ---
@@ -0,0 +1,311 @@
+package brooklyn.management.ha;
+
+import static org.testng.Assert.assertEquals;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import brooklyn.entity.basic.ApplicationBuilder;
+import brooklyn.entity.basic.Entities;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.entity.rebind.persister.BrooklynMementoPersisterToObjectStore;
+import brooklyn.entity.rebind.persister.InMemoryObjectStore;
+import brooklyn.entity.rebind.persister.ListeningObjectStore;
+import brooklyn.entity.rebind.persister.PersistMode;
+import brooklyn.entity.rebind.persister.PersistenceObjectStore;
+import brooklyn.location.Location;
+import brooklyn.management.internal.ManagementContextInternal;
+import brooklyn.test.entity.LocalManagementContextForTests;
+import brooklyn.test.entity.TestApplication;
+import brooklyn.util.collections.MutableList;
+import brooklyn.util.collections.MutableMap;
+import brooklyn.util.repeat.Repeater;
+import brooklyn.util.time.Duration;
+import brooklyn.util.time.Time;
+
+import com.google.common.base.Ticker;
+import com.google.common.collect.ImmutableList;
+
+@Test
+public class HighAvailabilityManagerSplitBrainTest {
+
+ private static final Logger log = LoggerFactory.getLogger(HighAvailabilityManagerSplitBrainTest.class);
+
+ private List<HaMgmtNode> nodes = new MutableList<HighAvailabilityManagerSplitBrainTest.HaMgmtNode>();
+ Map<String,String> sharedBackingStore = MutableMap.of();
+ Map<String,Date> sharedBackingStoreDates = MutableMap.of();
+ private AtomicLong sharedTime; // used to set the ticker's return value
+ private ClassLoader classLoader = getClass().getClassLoader();
+
+ public class HaMgmtNode {
+
+ private ManagementContextInternal mgmt;
+ private String ownNodeId;
+ private String nodeName;
+ private ListeningObjectStore objectStore;
+ private ManagementPlaneSyncRecordPersister persister;
+ private HighAvailabilityManagerImpl ha;
+ private Ticker ticker;
+ private AtomicLong currentTime; // used to set the ticker's return value
+
+ @BeforeMethod(alwaysRun=true)
+ public void setUp() throws Exception {
+ if (sharedTime==null)
+ currentTime = new AtomicLong(System.currentTimeMillis());
+
+ ticker = new Ticker() {
+ // strictly not a ticker because returns millis UTC, but it works fine even so
+ @Override public long read() {
+ if (sharedTime!=null) return sharedTime.get();
+ return currentTime.get();
+ }
+ };
+
+ nodeName = "node "+nodes.size();
+ mgmt = newLocalManagementContext();
+ ownNodeId = mgmt.getManagementNodeId();
+ objectStore = new ListeningObjectStore(newPersistenceObjectStore());
+ objectStore.injectManagementContext(mgmt);
+ objectStore.prepareForSharedUse(PersistMode.CLEAN, HighAvailabilityMode.DISABLED);
+ persister = new ManagementPlaneSyncRecordPersisterToObjectStore(mgmt, objectStore, classLoader);
+ ((ManagementPlaneSyncRecordPersisterToObjectStore)persister).allowRemoteTimestampInMemento();
+ BrooklynMementoPersisterToObjectStore persisterObj = new BrooklynMementoPersisterToObjectStore(objectStore, classLoader);
+ mgmt.getRebindManager().setPersister(persisterObj);
+ ha = new HighAvailabilityManagerImpl(mgmt)
+ .setPollPeriod(Duration.PRACTICALLY_FOREVER)
+ .setHeartbeatTimeout(Duration.THIRTY_SECONDS)
+ .setLocalTicker(ticker)
+ .setRemoteTicker(ticker)
+ .setPersister(persister);
+ log.info("Created "+nodeName+" "+ownNodeId);
+ }
+
+ public void tearDown() throws Exception {
+ if (ha != null) ha.stop();
+ if (mgmt != null) Entities.destroyAll(mgmt);
+ if (objectStore != null) objectStore.deleteCompletely();
+ }
+
+ private long tickerCurrentMillis() {
+ return ticker.read();
+ }
+
+ private long tickerAdvance(Duration duration) {
+ if (sharedTime!=null)
+ throw new IllegalStateException("Using shared ticker; cannot advance private node clock");
+ currentTime.addAndGet(duration.toMilliseconds());
+ return tickerCurrentMillis();
+ }
+
+ @Override
+ public String toString() {
+ return nodeName+" "+ownNodeId;
+ }
+ }
+
+ @BeforeMethod(alwaysRun=true)
+ public void setUp() throws Exception {
+ nodes.clear();
+ sharedBackingStore.clear();
+ }
+
+ public HaMgmtNode newNode() throws Exception {
+ HaMgmtNode node = new HaMgmtNode();
+ node.setUp();
+ nodes.add(node);
+ return node;
+ }
+
+ @AfterMethod(alwaysRun=true)
+ public void tearDown() throws Exception {
+ for (HaMgmtNode n: nodes)
+ n.tearDown();
+ }
+
+ private void sharedTickerAdvance(Duration duration) {
+ if (sharedTime==null) {
+ for (HaMgmtNode n: nodes)
+ n.tickerAdvance(duration);
+ } else {
+ sharedTime.addAndGet(duration.toMilliseconds());
+ }
+ }
+
+ private long sharedTickerCurrentMillis() {
+ return sharedTime.get();
+ }
+
+ protected void useSharedTime() {
+ if (!nodes.isEmpty())
+ throw new IllegalStateException("shared time must be set up before any nodes created");
+ sharedTime = new AtomicLong(System.currentTimeMillis());
+ }
+
+ protected ManagementContextInternal newLocalManagementContext() {
+ return new LocalManagementContextForTests();
+ }
+
+ protected PersistenceObjectStore newPersistenceObjectStore() {
+ return new InMemoryObjectStore(sharedBackingStore, sharedBackingStoreDates);
+ }
+
+ @Test
+ public void testIfNodeStopsBeingAbleToWrite() throws Exception {
+ useSharedTime();
+ HaMgmtNode n1 = newNode();
+ HaMgmtNode n2 = newNode();
+
+ n1.ha.start(HighAvailabilityMode.AUTO);
+ ManagementPlaneSyncRecord memento1 = n1.ha.getManagementPlaneSyncState();
+
+ log.info(n1+" HA: "+memento1);
+ assertEquals(memento1.getMasterNodeId(), n1.ownNodeId);
+ Long time0 = sharedTickerCurrentMillis();
+ assertEquals(memento1.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time0);
+ assertEquals(memento1.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+
+ n2.ha.start(HighAvailabilityMode.AUTO);
+ ManagementPlaneSyncRecord memento2 = n2.ha.getManagementPlaneSyncState();
+
+ log.info(n2+" HA: "+memento2);
+ assertEquals(memento2.getMasterNodeId(), n1.ownNodeId);
+ assertEquals(memento2.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+ assertEquals(memento2.getManagementNodes().get(n2.ownNodeId).getStatus(), ManagementNodeState.STANDBY);
+ assertEquals(memento2.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time0);
+ assertEquals(memento2.getManagementNodes().get(n2.ownNodeId).getRemoteTimestamp(), time0);
+
+ // and no entities at either
+ assertEquals(n1.mgmt.getApplications().size(), 0);
+ assertEquals(n2.mgmt.getApplications().size(), 0);
+
+ // create
+ TestApplication app = ApplicationBuilder.newManagedApp(EntitySpec.create(TestApplication.class), n1.mgmt);
+ app.start(ImmutableList.<Location>of());
+ app.setAttribute(TestApplication.MY_ATTRIBUTE, "hello");
+
+ assertEquals(n1.mgmt.getApplications().size(), 1);
+ assertEquals(n2.mgmt.getApplications().size(), 0);
+ n1.mgmt.getRebindManager().forcePersistNow();
+
+ n1.objectStore.setWritesFailSilently(true);
+ log.info(n1+" writes off");
+ sharedTickerAdvance(Duration.ONE_MINUTE);
+
+ n2.ha.publishAndCheck(false);
+ ManagementPlaneSyncRecord memento2b = n2.ha.getManagementPlaneSyncState();
+ log.info(n2+" HA now: "+memento2b);
+ Long time1 = sharedTickerCurrentMillis();
+
+ // n2 infers n1 as failed
+ assertEquals(memento2b.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.FAILED);
+ assertEquals(memento2b.getManagementNodes().get(n2.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+ assertEquals(memento2b.getMasterNodeId(), n2.ownNodeId);
+ assertEquals(memento2b.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time0);
+ assertEquals(memento2b.getManagementNodes().get(n2.ownNodeId).getRemoteTimestamp(), time1);
+
+ assertEquals(n1.mgmt.getApplications().size(), 1);
+ assertEquals(n2.mgmt.getApplications().size(), 1);
+ assertEquals(n1.mgmt.getApplications().iterator().next().getAttribute(TestApplication.MY_ATTRIBUTE), "hello");
+
+ n1.objectStore.setWritesFailSilently(false);
+ log.info(n1+" writes on");
+
+ sharedTickerAdvance(Duration.ONE_SECOND);
+ Long time2 = sharedTickerCurrentMillis();
+
+ n1.ha.publishAndCheck(false);
+ ManagementPlaneSyncRecord memento1b = n1.ha.getManagementPlaneSyncState();
+ log.info(n1+" HA now: "+memento1b);
+
+ // n1 comes back and demotes himself
+ assertEquals(memento1b.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.STANDBY);
+ assertEquals(memento1b.getManagementNodes().get(n2.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+ assertEquals(memento1b.getMasterNodeId(), n2.ownNodeId);
+ assertEquals(memento1b.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time2);
+ assertEquals(memento1b.getManagementNodes().get(n2.ownNodeId).getRemoteTimestamp(), time1);
+
+ // n2 now sees itself as master, with n1 in standby again
+ ManagementPlaneSyncRecord memento2c = n2.ha.getManagementPlaneSyncState();
+ log.info(n2+" HA now: "+memento2c);
+ assertEquals(memento2c.getManagementNodes().get(n1.ownNodeId).getStatus(), ManagementNodeState.STANDBY);
+ assertEquals(memento2c.getManagementNodes().get(n2.ownNodeId).getStatus(), ManagementNodeState.MASTER);
+ assertEquals(memento2c.getMasterNodeId(), n2.ownNodeId);
+ assertEquals(memento2c.getManagementNodes().get(n1.ownNodeId).getRemoteTimestamp(), time2);
+ assertEquals(memento2c.getManagementNodes().get(n2.ownNodeId).getRemoteTimestamp(), time2);
+
+ // and no entities at n1
+ assertEquals(n1.mgmt.getApplications().size(), 0);
+ assertEquals(n2.mgmt.getApplications().size(), 1);
+ }
+
+ @Test(invocationCount=50)
+ public void testIfNodeStopsBeingAbleToWriteManyTimes() throws Exception {
+ testIfNodeStopsBeingAbleToWrite();
+ }
+
+ @Test
+ public void testSimultaneousStartup() throws Exception {
+ doTestConcurrentStartup(5, null);
+ }
+
+ @Test
+ public void testNearSimultaneousStartup() throws Exception {
+ doTestConcurrentStartup(20, Duration.millis(20));
+ }
+
+ @Test(invocationCount=50, groups="Integration")
+ public void testNearSimultaneousStartupManyTimes() throws Exception {
+ doTestConcurrentStartup(20, Duration.millis(20));
+ }
+
+ protected void doTestConcurrentStartup(int size, final Duration staggerStart) throws Exception {
+ useSharedTime();
+
+ List<Thread> spawned = MutableList.of();
+ for (int i=0; i<size; i++) {
+ final HaMgmtNode n = newNode();
+ Thread t = new Thread() { public void run() {
+ if (staggerStart!=null) Time.sleep(staggerStart.multiply(Math.random()));
+ n.ha.start(HighAvailabilityMode.AUTO);
+ } };
+ spawned.add(t);
+ t.start();
+ }
+
+ Assert.assertTrue(Repeater.create().every(Duration.millis(1)).limitTimeTo(Duration.TEN_SECONDS).until(new Callable<Boolean>() {
--- End diff --
Why use repeater rather than:
Asserts.succeedsEventually(new Runnable() {
public void run() {
....
String errmsg = nodes.size()+" nodes: "+masters+" masters + "+standbys+" standbys; "
+ memento.getManagementNodes().size()+" saved, "
+ memento.getMasterNodeId()+" master, "+savedMasters+" savedMasters + "+savedStandbys+" savedStandbys"
assertEquals(masters, 1, errmsg);
assertEquals(standbys, nodes.size()-1, errmsg);
assertEquals(savedMasters, 1, errmsg);
assertEquals(savedStandbys, nodes.size-1, errmsg);
}});
This will save you getting a log message every 1ms as well (just get the testng message at the end if it fails).
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: HA uses shared timestamp (last mo...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/12#discussion_r14235192
--- Diff: core/src/main/java/brooklyn/management/ha/HighAvailabilityManagerImpl.java ---
@@ -384,24 +418,39 @@ protected void checkMaster(boolean initializing) {
return;
}
+ if (demotingSelfInFavourOfOtherMaster) {
+ LOG.debug("Master-change for this node only, demoting "+ownNodeMemento.toVerboseString()+" in favour of official master "+newMasterRecord);
+ demoteToStandby();
+ return;
+ }
+
// Need to choose a new master
- ManagementNodeSyncRecord newMasterRecord = masterChooser.choose(memento, heartbeatTimeout, ownNodeId, now);
+ newMasterRecord = masterChooser.choose(memento, heartbeatTimeout, ownNodeId);
+
String newMasterNodeId = (newMasterRecord == null) ? null : newMasterRecord.getNodeId();
URI newMasterNodeUri = (newMasterRecord == null) ? null : newMasterRecord.getUri();
boolean newMasterIsSelf = ownNodeId.equals(newMasterNodeId);
- LOG.debug("Management node master-promotion required: newMaster={}; oldMaster={}; plane={}, self={}; heartbeatTimeout={}",
- new Object[] {
- (newMasterRecord == null ? "<none>" : newMasterRecord.toVerboseString()),
- (masterNodeMemento == null ? masterNodeId+" (no memento)": masterNodeMemento.toVerboseString()),
- memento,
- ownNodeMemento.toVerboseString(),
- heartbeatTimeout
- });
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Management node master-change required: newMaster={}; oldMaster={}; plane={}, self={}; heartbeatTimeout={}",
+ new Object[] {
+ (newMasterRecord == null ? "<none>" : newMasterRecord.toVerboseString()),
+ (masterNodeMemento == null ? masterNodeId+" (no memento)": masterNodeMemento.toVerboseString()),
+ memento,
+ ownNodeMemento.toVerboseString(),
+ heartbeatTimeout
+ });
+ }
if (!initializing) {
- LOG.warn("HA subsystem detected change of master from "+masterNodeId+" to "
- + (newMasterNodeId == null ? "<unknown>" :
- newMasterNodeId + (newMasterNodeUri!=null ? " "+newMasterNodeUri : "")));
+ if (!demotingSelfInFavourOfOtherMaster) {
--- End diff --
should have returned above if `demotingSelfInFavourOfOtherMaster`, but no harm checking again.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---