HBASE-16640 TimeoutBlockingQueue#remove() should return whether the entry is removed

This commit is contained in:
tedyu 2016-09-15 17:34:23 -07:00
parent 8c4b09dfba
commit e782d0bbdf
3 changed files with 16 additions and 10 deletions

View File

@ -615,9 +615,10 @@ public class ProcedureExecutor<TEnvironment> {
/** /**
* Remove a chore procedure from the executor * Remove a chore procedure from the executor
* @param chore the chore to remove * @param chore the chore to remove
* @return whether the chore is removed
*/ */
public void removeChore(final ProcedureInMemoryChore chore) { public boolean removeChore(final ProcedureInMemoryChore chore) {
waitingTimeout.remove(chore); return waitingTimeout.remove(chore);
} }
/** /**

View File

@ -92,15 +92,17 @@ public class TimeoutBlockingQueue<E> {
} }
} }
public void remove(E e) { public boolean remove(E e) {
if (e == null) return false;
lock.lock(); lock.lock();
try { try {
for (int i = 0; i < objects.length; ++i) { for (int i = 0; i < objects.length; ++i) {
if (objects[i] == e) { if (e.equals(objects[i])) {
objects[i] = null; objects[i] = null;
return; return true;
} }
} }
return false;
} finally { } finally {
lock.unlock(); lock.unlock();
} }

View File

@ -137,18 +137,21 @@ public class TestTimeoutBlockingQueue {
TimeoutBlockingQueue<TestObject> queue = TimeoutBlockingQueue<TestObject> queue =
new TimeoutBlockingQueue<TestObject>(2, new TestObjectTimeoutRetriever()); new TimeoutBlockingQueue<TestObject>(2, new TestObjectTimeoutRetriever());
TestObject[] objs = new TestObject[5]; final int effectiveLen = 5;
for (int i = 0; i < objs.length; ++i) { TestObject[] objs = new TestObject[6];
for (int i = 0; i < effectiveLen; ++i) {
objs[i] = new TestObject(0, i * 10); objs[i] = new TestObject(0, i * 10);
queue.add(objs[i]); queue.add(objs[i]);
} }
objs[effectiveLen] = new TestObject(0, effectiveLen * 10);
queue.dump(); queue.dump();
for (int i = 0; i < objs.length; i += 2) { for (int i = 0; i < effectiveLen; i += 2) {
queue.remove(objs[i]); assertTrue(queue.remove(objs[i]));
} }
assertTrue(!queue.remove(objs[effectiveLen]));
for (int i = 0; i < objs.length; ++i) { for (int i = 0; i < effectiveLen; ++i) {
TestObject x = queue.poll(); TestObject x = queue.poll();
assertEquals((i % 2) == 0 ? null : objs[i], x); assertEquals((i % 2) == 0 ? null : objs[i], x);
} }