Skip to Content.
Sympa Menu

perfsonar-dev - perfsonar: r5444 - in trunk/ps-mdm-lsclient-impl/src: main/java/org/perfsonar/ri/lsclient/level1 main/java/org/perfsonar/ri/lsclient/utils test/java/org/perfsonar/ri/lsclient/utils

Subject: perfsonar development work

List archive

perfsonar: r5444 - in trunk/ps-mdm-lsclient-impl/src: main/java/org/perfsonar/ri/lsclient/level1 main/java/org/perfsonar/ri/lsclient/utils test/java/org/perfsonar/ri/lsclient/utils


Chronological Thread 
  • From:
  • To:
  • Subject: perfsonar: r5444 - in trunk/ps-mdm-lsclient-impl/src: main/java/org/perfsonar/ri/lsclient/level1 main/java/org/perfsonar/ri/lsclient/utils test/java/org/perfsonar/ri/lsclient/utils
  • Date: Wed, 30 Dec 2009 09:08:37 -0500

Author: krzjed
Date: 2009-12-30 09:08:37 -0500 (Wed, 30 Dec 2009)
New Revision: 5444

Modified:

trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/level1/RootGlses.java

trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/Coordinator.java

trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/ResultCollector.java

trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/ResultContainer.java

trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/SortedResultCollector.java

trunk/ps-mdm-lsclient-impl/src/test/java/org/perfsonar/ri/lsclient/utils/SortedResultCollectorTest.java
Log:
javadoc
some refactoring

Modified:
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/level1/RootGlses.java
===================================================================
---
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/level1/RootGlses.java
2009-12-30 13:40:19 UTC (rev 5443)
+++
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/level1/RootGlses.java
2009-12-30 14:08:37 UTC (rev 5444)
@@ -43,7 +43,7 @@
long startTime = System.nanoTime();
connection.connect();
logger.info(service + " timed fo " +
Long.valueOf(System.nanoTime() - startTime) + "ns.");
- collector.addPair(Long.valueOf(System.nanoTime() -
startTime), service);
+ collector.addElement(Long.valueOf(System.nanoTime()
- startTime), service);
} catch (MalformedURLException e) {
logger.error("Supplied service endpoint('" +
service.getEndPoint()
+ "') is not a valid URL, this client only
understands http(s)", e);

Modified:
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/Coordinator.java
===================================================================
---
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/Coordinator.java
2009-12-30 13:40:19 UTC (rev 5443)
+++
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/Coordinator.java
2009-12-30 14:08:37 UTC (rev 5444)
@@ -1,18 +1,46 @@
package org.perfsonar.ri.lsclient.utils;

+/**
+ * Class for coordinating parallel, redundant operations
+ *
+ * @author krzjed
+ *
+ */
public class Coordinator {
private boolean processingFinished;
private int maxPoolSize;
private int used = 0;

+ /**
+ * Constructor creating object that allows
{@link
Integer#MAX_VALUE} of
+ * parallel operations
+ */
public Coordinator() {
this(Integer.MAX_VALUE);
}

+ /**
+ * Constructor creating object that allows given number of parallel
+ * operations
+ *
+ * @param size
+ * number of operations allowed to be executed in parallel
+ */
public Coordinator(Integer size) {
this.maxPoolSize = size;
}

+ /**
+ * Tests if operation should be performed (any other operation started
+ * earlier hasn't found a result).<br />
+ * If parallel operations limit has been reached current thread is
blocked
+ * until one of currently executed operations ends.
+ *
+ * @return <code>true</code> (result hasn't been found yet),
+ * <code>false</code> result has been found
+ * @throws InterruptedException
+ *
{@link
Object#wait()} operation has been interrupted.
+ */
public synchronized boolean doContinue() throws InterruptedException {
if (this.processingFinished) {
return false;
@@ -27,11 +55,19 @@
return true;
}

+ /**
+ * Method that should be called to indicate that one operation has
finished
+ * processing
+ */
public synchronized void finished() {
this.used--;
this.notifyAll();
}

+ /**
+ * Method that should be called to indicate that result has been found
(any
+ * other operation shouldn't be started)
+ */
public synchronized void processingFinished() {
this.processingFinished = true;
this.notifyAll();

Modified:
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/ResultCollector.java
===================================================================
---
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/ResultCollector.java
2009-12-30 13:40:19 UTC (rev 5443)
+++
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/ResultCollector.java
2009-12-30 14:08:37 UTC (rev 5444)
@@ -6,33 +6,72 @@

import org.apache.log4j.Logger;

+/**
+ * Class for collecting results from parallel requests
+ *
+ * @author krzjed
+ *
+ * @param <T>
+ * type of result
+ */
public class ResultCollector<T> {
private static Logger logger = Logger.getLogger(ResultCollector.class);
private int required;
private Set<T> resultSet = new HashSet<T>();

+ /**
+ * Creates instance that not requires results added
+ */
public ResultCollector() {
this(0);
}

+ /**
+ * Creates instance that requires <b>i</b> responses from requests
+ *
+ * @param i
+ * number of required responses
+ */
public ResultCollector(int i) {
this.required = i;
}

+ /**
+ * Adds element to result set. Indicates finish of processing by a
request
+ * thread.
+ *
+ * @param element
+ * to add
+ */
public synchronized void addElement(T element) {
this.resultSet.add(element);
this.required--;
this.testRequirements();
- logger.info(element + " addet to ResultCollector");
+ logger.info(element + " added to ResultCollector");
}

- public synchronized void addAllElements(Collection<? extends T> element)
{
- this.resultSet.addAll(element);
+ /**
+ * Adds elements of given set to result set. Indicates finish of
processing
+ * by a request thread.
+ *
+ * @param elements
+ * to add
+ */
+ public synchronized void addAllElements(Collection<? extends T>
elements) {
+ this.resultSet.addAll(elements);
this.required--;
this.testRequirements();
- logger.info(element + " addet to ResultCollector");
+ logger.info(elements + " added to ResultCollector");
}

+ /**
+ * Returns result set. If not all requests threads signaled end of
+ * processing current thread wait until.
+ *
+ * @return set of results from all threads
+ * @throws InterruptedException
+ *
{@link
Object#wait()} operation has been interrupted.
+ */
public synchronized Set<T> getResultSet() throws InterruptedException {
while (required > 0) {
this.wait();
@@ -40,15 +79,24 @@
return this.resultSet;
}

+ /**
+ * Signals that current request thread hasn't got result to return.
+ */
public synchronized void signalLackOfElement() {
this.required--;
this.testRequirements();
}

+ /**
+ * Signals that more responses is expected
+ */
public synchronized void signalMoreElements() {
this.required++;
}

+ /**
+ * Tests if requirements of processing finish are met
+ */
private void testRequirements() {
if (this.required <= 0) {
this.notifyAll();

Modified:
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/ResultContainer.java
===================================================================
---
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/ResultContainer.java
2009-12-30 13:40:19 UTC (rev 5443)
+++
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/ResultContainer.java
2009-12-30 14:08:37 UTC (rev 5444)
@@ -2,11 +2,26 @@

import org.apache.log4j.Logger;

+/**
+ * Class for retrieving result from parallel threads.
+ *
+ * @author krzjed
+ *
+ * @param <T>
+ * type of result
+ */
public class ResultContainer<T> {
private T value;
private boolean valueSet = false;
private static Logger logger = Logger.getLogger(ResultContainer.class);

+ /**
+ * Returns result. If it hasn't been set causes current thread to wait
+ *
+ * @return result
+ * @throws InterruptedException
+ *
{@link
Object#wait()} operation has been interrupted.
+ */
public synchronized T getValue() throws InterruptedException {
while (!this.valueSet) {
this.wait();
@@ -14,6 +29,13 @@
return this.value;
}

+ /**
+ * Sets value that should be returned by
{@link
ResultContainer#getValue()}
+ * method. Only value passed on first call is stored.
+ *
+ * @param value
+ * to store
+ */
public synchronized void setValue(T value) {
if (!this.valueSet) {
this.value = value;

Modified:
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/SortedResultCollector.java
===================================================================
---
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/SortedResultCollector.java
2009-12-30 13:40:19 UTC (rev 5443)
+++
trunk/ps-mdm-lsclient-impl/src/main/java/org/perfsonar/ri/lsclient/utils/SortedResultCollector.java
2009-12-30 14:08:37 UTC (rev 5444)
@@ -7,31 +7,76 @@

import org.apache.log4j.Logger;

+/**
+ * Class for collecting results from parallel requests sorting them by a
given
+ * key
+ *
+ * @author krzjed
+ *
+ * @param <K>
+ * sort key type
+ * @param <V>
+ * value type
+ */
public class SortedResultCollector<K, V> {
private static Logger logger = Logger.getLogger(ResultCollector.class);
private int required;
private SortedMap<K, V> resultMap = new TreeMap<K, V>();

+ /**
+ * Creates instance that not requires results added
+ */
public SortedResultCollector() {
this(0);
}

+ /**
+ * Creates instance that requires <b>i</b> responses from requests
+ *
+ * @param i
+ * number of required responses
+ */
public SortedResultCollector(int i) {
this.required = i;
}

- public synchronized void addPair(K key, V value) {
+ /**
+ * Adds element to result set. Indicates finish of processing by a
request
+ * thread.
+ *
+ * @param key
+ * of sorting
+ * @param value
+ * to add
+ */
+ public synchronized void addElement(K key, V value) {
this.resultMap.put(key, value);
this.required--;
this.testRequirements();
}

- public synchronized void addAllPairs(Map<? extends K, ? extends V> map) {
+ /**
+ * Adds map <key,value> to result set. Indicates finish of processing by
a
+ * request thread.
+ *
+ * @param map
+ * in which key is <i>key</i> of sorting and <i>value</i> is
+ * value to add.
+ */
+ public synchronized void addAllElements(Map<? extends K, ? extends V>
map) {
this.resultMap.putAll(map);
this.required--;
this.testRequirements();
}

+ /**
+ * Returns map added results. If not all requests threads signaled end of
+ * processing current thread wait until.
+ *
+ * @return map of pairs <key of sorting, result value>
+ * @throws InterruptedException
+ *
{@link
Object#wait()} operation has been interrupted.
+ */
public synchronized SortedMap<K, V> getResultMap() throws
InterruptedException {
while (required > 0) {
this.wait();
@@ -39,6 +84,14 @@
return this.resultMap;
}

+ /**
+ * Returns result set. If not all requests threads signaled end of
+ * processing current thread wait until.
+ *
+ * @return set of results from all threads
+ * @throws InterruptedException
+ *
{@link
Object#wait()} operation has been interrupted.
+ */
public synchronized Collection<V> getValues() throws
InterruptedException {
while (required > 0) {
this.wait();
@@ -46,6 +99,13 @@
return this.resultMap.values();
}

+ /**
+ * Tests if result set is empty. If not all requests threads signaled
end of
+ * processing current thread wait until.
+ *
+ * @return <code>true</code> if result set is empty, <code>false</code>
if
+ * not
+ */
public synchronized boolean isEmpty() {
while (required > 0) {
try {
@@ -57,15 +117,24 @@
return this.resultMap.isEmpty();
}

+ /**
+ * Signals that current request thread hasn't got result to return.
+ */
public synchronized void signalLackOfElement() {
this.required--;
this.testRequirements();
}

+ /**
+ * Signals that one more response is expected
+ */
public synchronized void signalMoreElements() {
this.required++;
}

+ /**
+ * Tests if requirements of processing finish are met
+ */
private void testRequirements() {
if (this.required <= 0) {
this.notifyAll();

Modified:
trunk/ps-mdm-lsclient-impl/src/test/java/org/perfsonar/ri/lsclient/utils/SortedResultCollectorTest.java
===================================================================
---
trunk/ps-mdm-lsclient-impl/src/test/java/org/perfsonar/ri/lsclient/utils/SortedResultCollectorTest.java
2009-12-30 13:40:19 UTC (rev 5443)
+++
trunk/ps-mdm-lsclient-impl/src/test/java/org/perfsonar/ri/lsclient/utils/SortedResultCollectorTest.java
2009-12-30 14:08:37 UTC (rev 5444)
@@ -19,7 +19,7 @@
@Test(timeout = 100)
public void addSingleElementTest() throws InterruptedException {
this.src.signalMoreElements();
- this.src.addPair(2, "sss");
+ this.src.addElement(2, "sss");
Collection<String> res = src.getValues();
Assert.assertEquals(1, res.size());
Assert.assertTrue(res.contains("sss"));
@@ -29,9 +29,9 @@
public void addManyElementsTest() throws InterruptedException {
this.src = new SortedResultCollector<Integer, String>(3);
this.src.signalMoreElements();
- this.src.addPair(3, "c");
- this.src.addPair(4, "d");
- this.src.addPair(2, "b");
+ this.src.addElement(3, "c");
+ this.src.addElement(4, "d");
+ this.src.addElement(2, "b");
this.src.signalLackOfElement();
Collection<String> res = src.getValues();
Assert.assertEquals(3, res.size());
@@ -50,7 +50,7 @@
Thread t = new Thread() {
@Override
public void run() {
- src.addPair(a, "a" + a);
+ src.addElement(a, "a" + a);
}
};
t.start();



  • perfsonar: r5444 - in trunk/ps-mdm-lsclient-impl/src: main/java/org/perfsonar/ri/lsclient/level1 main/java/org/perfsonar/ri/lsclient/utils test/java/org/perfsonar/ri/lsclient/utils, svnlog, 12/30/2009

Archive powered by MHonArc 2.6.16.

Top of Page