Permalink
Browse files

introducing StabilityStatistics

  • Loading branch information...
1 parent 385cadb commit 225df0a457ce21faf9a3dab3455b9654a5ce3194 @ropalka ropalka committed Jan 15, 2013
@@ -22,11 +22,17 @@
package org.jboss.msc.service;
+import static java.lang.Thread.holdsLock;
+import static org.jboss.msc.service.ServiceController.Mode.ACTIVE;
+import static org.jboss.msc.service.ServiceController.Mode.LAZY;
+import static org.jboss.msc.service.ServiceController.Mode.NEVER;
+import static org.jboss.msc.service.ServiceController.Mode.ON_DEMAND;
+import static org.jboss.msc.service.ServiceController.Mode.PASSIVE;
+import static org.jboss.msc.service.ServiceController.Mode.REMOVE;
+
import java.util.Set;
import java.util.concurrent.TimeUnit;
-import static java.lang.Thread.holdsLock;
-
/**
* A stability monitor for satisfying certain AS use cases.
*
@@ -38,43 +44,72 @@
private final Object lock = new Object();
private final Set<ServiceController<?>> problems = new IdentityHashSet<ServiceController<?>>();
private final Set<ServiceController<?>> failed = new IdentityHashSet<ServiceController<?>>();
+ private Set<ServiceControllerImpl<?>> controllers = new IdentityHashSet<ServiceControllerImpl<?>>();
private int unstableServices;
-
+
public void addController(final ServiceController<?> controller) {
if (controller == null) return;
final ServiceControllerImpl<?> serviceController = (ServiceControllerImpl<?>) controller;
serviceController.addMonitor(this);
+ synchronized (lock) {
+ controllers.add(serviceController);
+ }
}
public void removeController(final ServiceController<?> controller) {
if (controller == null) return;
final ServiceControllerImpl<?> serviceController = (ServiceControllerImpl<?>) controller;
serviceController.removeMonitor(this);
+ synchronized (lock) {
+ controllers.remove(serviceController);
+ }
+ }
+
+ public void clear() {
+ final Set<ServiceControllerImpl<?>> controllers;
+ synchronized(lock) {
+ controllers = this.controllers;
+ this.controllers = new IdentityHashSet<ServiceControllerImpl<?>>();
+ }
+ for (final ServiceControllerImpl<?> controller : controllers) {
+ controller.removeMonitor(this);
+ }
}
public void awaitStability() throws InterruptedException {
- awaitStability(null, null);
+ awaitStability(null, null, null);
+ }
+
+ public void awaitStability(final StabilityStatistics statistics) throws InterruptedException {
+ awaitStability(null, null, statistics);
}
public boolean awaitStability(final long timeout, final TimeUnit unit) throws InterruptedException {
- return awaitStability(timeout, unit, null, null);
+ return awaitStability(timeout, unit, null, null, null);
+ }
+
+ public boolean awaitStability(final long timeout, final TimeUnit unit, final StabilityStatistics statistics) throws InterruptedException {
+ return awaitStability(timeout, unit, null, null, statistics);
}
public void awaitStability(final Set<? super ServiceController<?>> failed, final Set<? super ServiceController<?>> problem) throws InterruptedException {
+ this.awaitStability(failed, problem, null);
+ }
+
+ public void awaitStability(final Set<? super ServiceController<?>> failed, final Set<? super ServiceController<?>> problems, final StabilityStatistics statistics) throws InterruptedException {
synchronized (lock) {
while (unstableServices != 0) {
lock.wait();
}
- if (failed != null) {
- failed.addAll(this.failed);
- }
- if (problem != null) {
- problem.addAll(this.problems);
- }
+ provideStatistics(failed, problems, statistics);
}
}
public boolean awaitStability(final long timeout, final TimeUnit unit, final Set<? super ServiceController<?>> failed, final Set<? super ServiceController<?>> problem) throws InterruptedException {
+ return awaitStability(timeout, unit, failed, problem, null);
+ }
+
+ public boolean awaitStability(final long timeout, final TimeUnit unit, final Set<? super ServiceController<?>> failed, final Set<? super ServiceController<?>> problems, final StabilityStatistics statistics) throws InterruptedException {
long now = System.nanoTime();
long remaining = unit.toNanos(timeout);
synchronized (lock) {
@@ -85,12 +120,7 @@ public boolean awaitStability(final long timeout, final TimeUnit unit, final Set
lock.wait(remaining / 1000000L, (int) (remaining % 1000000L));
remaining -= (-now + (now = System.nanoTime()));
}
- if (failed != null) {
- failed.addAll(this.failed);
- }
- if (problem != null) {
- problem.addAll(this.problems);
- }
+ provideStatistics(failed, problems, statistics);
return true;
}
}
@@ -137,4 +167,32 @@ void decrementUnstableServices() {
assert unstableServices >= 0;
}
}
+
+ private void provideStatistics(final Set<? super ServiceController<?>> failed, final Set<? super ServiceController<?>> problems, final StabilityStatistics statistics) {
+ assert holdsLock(lock);
+ if (failed != null) {
+ failed.addAll(this.failed);
+ }
+ if (problems != null) {
+ problems.addAll(this.problems);
+ }
+ if (statistics == null) return;
+ int active = 0, lazy = 0, onDemand = 0, never = 0, passive = 0, remove = 0;
+ for (final ServiceController<?> controller : controllers) {
+ if (controller.getMode() == ACTIVE) active++;
+ else if (controller.getMode() == PASSIVE) passive++;
+ else if (controller.getMode() == ON_DEMAND) onDemand++;
+ else if (controller.getMode() == NEVER) never++;
+ else if (controller.getMode() == LAZY) lazy++;
+ else if (controller.getMode() == REMOVE) remove++;
+ }
+ statistics.setActiveCount(active);
+ statistics.setFailCount(this.failed.size());
+ statistics.setLazyCount(lazy);
+ statistics.setOnDemandCount(onDemand);
+ statistics.setNeverCount(never);
+ statistics.setPassiveCount(passive);
+ statistics.setProblemCount(this.problems.size());
+ statistics.setRemoveCount(remove);
+ }
}
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2013, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.msc.service;
+
+/**
+ * A stability monitor statistics.
+ *
+ * @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
+ */
+public final class StabilityStatistics {
+
+ private int active;
+ private int fail;
+ private int lazy;
+ private int never;
+ private int onDemand;
+ private int passive;
+ private int problem;
+ private int remove;
+
+ public int getActiveCount() {
+ return active;
+ }
+
+ public int getFailCount() {
+ return fail;
+ }
+
+ public int getLazyCount() {
+ return lazy;
+ }
+
+ public int getNeverCount() {
+ return never;
+ }
+
+ public int getOnDemandCount() {
+ return onDemand;
+ }
+
+ public int getPassiveCount() {
+ return passive;
+ }
+
+ public int getProblemCount() {
+ return problem;
+ }
+
+ public int getRemoveCount() {
+ return remove;
+ }
+
+ void setActiveCount(final int count) {
+ active = count;
+ }
+
+ void setFailCount(final int count) {
+ fail = count;
+ }
+
+ void setLazyCount(final int count) {
+ lazy = count;
+ }
+
+ void setNeverCount(final int count) {
+ never = count;
+ }
+
+ void setOnDemandCount(final int count) {
+ onDemand = count;
+ }
+
+ void setPassiveCount(final int count) {
+ passive = count;
+ }
+
+ void setProblemCount(final int count) {
+ problem = count;
+ }
+
+ void setRemoveCount(final int count) {
+ remove = count;
+ }
+}

0 comments on commit 225df0a

Please sign in to comment.