[GitHub] thrift pull request #1337: THRIFT-4292: Implement TimerManager::remove()

2017-09-08 Thread asfgit
Github user asfgit closed the pull request at:

https://github.com/apache/thrift/pull/1337


---


[GitHub] thrift pull request #1337: THRIFT-4292: Implement TimerManager::remove()

2017-09-08 Thread Typz
Github user Typz commented on a diff in the pull request:

https://github.com/apache/thrift/pull/1337#discussion_r137811890
  
--- Diff: lib/cpp/src/thrift/concurrency/TimerManager.cpp ---
@@ -290,11 +292,23 @@ void TimerManager::add(shared_ptr task, 
const struct timeval& value) {
 }
 
 void TimerManager::remove(shared_ptr task) {
-  (void)task;
   Synchronized s(monitor_);
   if (state_ != TimerManager::STARTED) {
 throw IllegalStateException();
   }
+  std::vector toRemove;
--- End diff --

done


---


[GitHub] thrift pull request #1337: THRIFT-4292: Implement TimerManager::remove()

2017-09-08 Thread Typz
Github user Typz commented on a diff in the pull request:

https://github.com/apache/thrift/pull/1337#discussion_r137810110
  
--- Diff: lib/cpp/test/concurrency/TimerManagerTests.h ---
@@ -126,6 +126,38 @@ class TimerManagerTests {
 return true;
   }
 
+  /**
+   * This test creates two tasks, removes the first one then waits for the 
second one. It then
+   * verifies that the timer manager properly clean up itself and the 
remaining orphaned timeout
+   * task when the manager goes out of scope and its destructor is called.
+   */
+  bool test01(int64_t timeout = 1000LL) {
+TimerManager timerManager;
+timerManager.threadFactory(shared_ptr(new 
PlatformThreadFactory()));
+timerManager.start();
+assert(timerManager.state() == TimerManager::STARTED);
+
+Synchronized s(_monitor);
+
+// Setup the two tasks
+shared_ptr taskToRemove
+  = shared_ptr(new 
TimerManagerTests::Task(_monitor, timeout / 2));
+timerManager.add(taskToRemove, taskToRemove->_timeout);
+
+shared_ptr task
+  = shared_ptr(new 
TimerManagerTests::Task(_monitor, timeout));
+timerManager.add(task, task->_timeout);
+
+// Remove one task and wait until the other has completed
+timerManager.remove(taskToRemove);
+_monitor.wait(timeout * 2);
+
+assert(!taskToRemove->_done);
+assert(task->_done);
+
+return true;
+  }
+
--- End diff --

done


---


[GitHub] thrift pull request #1337: THRIFT-4292: Implement TimerManager::remove()

2017-09-08 Thread Typz
Github user Typz commented on a diff in the pull request:

https://github.com/apache/thrift/pull/1337#discussion_r137809132
  
--- Diff: lib/cpp/src/thrift/concurrency/TimerManager.cpp ---
@@ -52,6 +52,8 @@ class TimerManager::Task : public Runnable {
 }
   }
 
+  shared_ptr runnable() const { return runnable_; }
--- End diff --

done


---


[GitHub] thrift pull request #1337: THRIFT-4292: Implement TimerManager::remove()

2017-09-08 Thread Typz
Github user Typz commented on a diff in the pull request:

https://github.com/apache/thrift/pull/1337#discussion_r137808236
  
--- Diff: lib/cpp/src/thrift/concurrency/TimerManager.cpp ---
@@ -290,11 +292,23 @@ void TimerManager::add(shared_ptr task, 
const struct timeval& value) {
 }
 
 void TimerManager::remove(shared_ptr task) {
-  (void)task;
   Synchronized s(monitor_);
   if (state_ != TimerManager::STARTED) {
 throw IllegalStateException();
   }
+  std::vector toRemove;
+  for (task_iterator ix = taskMap_.begin(); ix != taskMap_.end(); ++ix) {
+if (ix->second->runnable() == task) {
+  toRemove.push_back(ix);
+}
+  }
+  if (toRemove.empty()) {
+throw NoSuchTaskException();
+  }
+  for (std::vector::iterator ix = toRemove.begin(); ix != 
toRemove.end(); ++ix) {
+taskMap_.erase(*ix);
--- End diff --

Tasks cannot exist multiple times, only newly allocated tasks are inserted:
```cpp
  taskMap_.insert(
std::pair(timeout, shared_ptr(new 
Task(task;
```

Which is why I implemented it in the same fashion as the run() method. But 
i can change it.


---


[GitHub] thrift pull request #1337: THRIFT-4292: Implement TimerManager::remove()

2017-09-05 Thread jeking3
Github user jeking3 commented on a diff in the pull request:

https://github.com/apache/thrift/pull/1337#discussion_r137163331
  
--- Diff: lib/cpp/test/concurrency/TimerManagerTests.h ---
@@ -126,6 +126,38 @@ class TimerManagerTests {
 return true;
   }
 
+  /**
+   * This test creates two tasks, removes the first one then waits for the 
second one. It then
+   * verifies that the timer manager properly clean up itself and the 
remaining orphaned timeout
+   * task when the manager goes out of scope and its destructor is called.
+   */
+  bool test01(int64_t timeout = 1000LL) {
+TimerManager timerManager;
+timerManager.threadFactory(shared_ptr(new 
PlatformThreadFactory()));
+timerManager.start();
+assert(timerManager.state() == TimerManager::STARTED);
+
+Synchronized s(_monitor);
+
+// Setup the two tasks
+shared_ptr taskToRemove
+  = shared_ptr(new 
TimerManagerTests::Task(_monitor, timeout / 2));
+timerManager.add(taskToRemove, taskToRemove->_timeout);
+
+shared_ptr task
+  = shared_ptr(new 
TimerManagerTests::Task(_monitor, timeout));
+timerManager.add(task, task->_timeout);
+
+// Remove one task and wait until the other has completed
+timerManager.remove(taskToRemove);
+_monitor.wait(timeout * 2);
+
+assert(!taskToRemove->_done);
+assert(task->_done);
+
+return true;
+  }
+
--- End diff --

Recommend you add a test to remove the same task that's been added to the 
timerManager more than once.


---


[GitHub] thrift pull request #1337: THRIFT-4292: Implement TimerManager::remove()

2017-09-05 Thread jeking3
Github user jeking3 commented on a diff in the pull request:

https://github.com/apache/thrift/pull/1337#discussion_r137162542
  
--- Diff: lib/cpp/src/thrift/concurrency/TimerManager.cpp ---
@@ -290,11 +292,23 @@ void TimerManager::add(shared_ptr task, 
const struct timeval& value) {
 }
 
 void TimerManager::remove(shared_ptr task) {
-  (void)task;
   Synchronized s(monitor_);
   if (state_ != TimerManager::STARTED) {
 throw IllegalStateException();
   }
+  std::vector toRemove;
+  for (task_iterator ix = taskMap_.begin(); ix != taskMap_.end(); ++ix) {
+if (ix->second->runnable() == task) {
+  toRemove.push_back(ix);
+}
+  }
+  if (toRemove.empty()) {
+throw NoSuchTaskException();
+  }
+  for (std::vector::iterator ix = toRemove.begin(); ix != 
toRemove.end(); ++ix) {
+taskMap_.erase(*ix);
--- End diff --

As written, if a single task exists multiple times in the taskMap then this 
first erase call invalidates any other iterators in toRemove, and it will lead 
to undefined behavior. This must be fixed to merge; see my previous comment for 
a safer way to achieve this.


---


[GitHub] thrift pull request #1337: THRIFT-4292: Implement TimerManager::remove()

2017-09-05 Thread jeking3
Github user jeking3 commented on a diff in the pull request:

https://github.com/apache/thrift/pull/1337#discussion_r137163123
  
--- Diff: lib/cpp/src/thrift/concurrency/TimerManager.cpp ---
@@ -290,11 +292,23 @@ void TimerManager::add(shared_ptr task, 
const struct timeval& value) {
 }
 
 void TimerManager::remove(shared_ptr task) {
-  (void)task;
   Synchronized s(monitor_);
   if (state_ != TimerManager::STARTED) {
 throw IllegalStateException();
   }
+  std::vector toRemove;
--- End diff --

I'd recommend using the prefix/postfix method to walk and erase here in 
order to avoid iterator invalidation:

bool found = false;
for (task_iterator it = taskMap_.begin(); it != taskMap_.end(); ) {
  if (ix->second->runnable() == task) {
found = true;
taskMap_.erase(it++);
  } else {
++it;
  }
}

if (!found) {
  throw NoSuchTaskException();
}



---


[GitHub] thrift pull request #1337: THRIFT-4292: Implement TimerManager::remove()

2017-09-05 Thread jeking3
Github user jeking3 commented on a diff in the pull request:

https://github.com/apache/thrift/pull/1337#discussion_r137163110
  
--- Diff: lib/cpp/src/thrift/concurrency/TimerManager.cpp ---
@@ -52,6 +52,8 @@ class TimerManager::Task : public Runnable {
 }
   }
 
+  shared_ptr runnable() const { return runnable_; }
--- End diff --

Consider implementing an ``operator ==`` instead which compares to a 
``shared_ptr`` thus hiding the internal details of the 
``TimerManager::Task`` and simplifying the caller to ``if (task == 
ix->second)`` below.  What's there works, just a suggestion - not required to 
merge.


---


[GitHub] thrift pull request #1337: THRIFT-4292: Implement TimerManager::remove()

2017-08-28 Thread Typz
GitHub user Typz opened a pull request:

https://github.com/apache/thrift/pull/1337

THRIFT-4292: Implement TimerManager::remove()



You can merge this pull request into a Git repository by running:

$ git pull https://github.com/Typz/thrift THRIFT-4292

Alternatively you can review and apply these changes as the patch at:

https://github.com/apache/thrift/pull/1337.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 #1337


commit 177c9ea5a246f296857bb62288befa5acc295902
Author: Francois Ferrand 
Date:   2017-08-25T07:01:26Z

THRIFT-4292: Implement TimerManager::remove()




---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---