8000 fix improper use of iteration by pongad · Pull Request #1552 · googleapis/google-cloud-java · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@pongad
8000 Copy link
Contributor
@pongad pongad commented Jan 19, 2017

cc @davidtorres

The previous implementation modifies a HashMap while iterating through
its key set, causeing unpredictable iteration behavior.
This might be the reason our tests intermittently deadlock.

The new implementation uses a PriorityQueue.
The time complexity is O(M * log N),
where M is the number of expirations before the cut over time
and N is the total number of expirations.
I am not sure how this compares to O(N) intended in the previous
implementation.
If required, O(N) is also possible using an ArrayList.

Unfortunately, a new failure has emerged.
Instead of deadlocking, testModifyAckDeadline intermittently fails.
Maybe

  • I have fixed the old bug and created a new one,
  • I have fixed the old bug that was masking another one,
  • The deadlock wasn't caused by the iteration. Now the tests just fail
    before they could deadlock,
    or some combination thereof.
    The incorrect iteration should be fixed regardless.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Jan 19, 2017
synchronized (outstandingAckHandlers) {
addOutstadingAckHandlers(expiration, ackHandlers);
outstandingAckHandlers.add(
new ExtensionJob(expiration, INITIAL_ACK_DEADLINE_EXTENSION_SECONDS, ackHandlers));

This comment was marked as spam.

@garrettjonesgoogle
Copy link
Member

I think @davidtorres should probably do the first pass, since he wrote the original code and understands it better.

The previous implementation modifies a HashMap while iterating through
its key set, causeing unpredictable iteration behavior.
This might be the reason our tests intermittently deadlock.

The new implementation uses a PriorityQueue.
The time complexity is O(M * log N),
where M is the number of expirations before the cut over time
and N is the total number of expirations.
I am not sure how this compares to O(N) intended in the previous
implementation.
If required, O(N) is also possible using an ArrayList.

Unfortunately, a new failure has emerged.
Instead of deadlocking, testModifyAckDeadline intermittently fails.
Maybe
- I have fixed the old bug and created a new one,
- I have fixed the old bug that was masking another one,
- The deadlock wasn't caused by the iteration. Now the tests just fail
  before they could deadlock,
or some combination thereof.
The incorrect iteration should be fixed regardless.
don't process the same job twice

record next expiration after the loop
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.02%) to 83.134% when pulling 5c094fa on pongad:fix-iter into 0550871 on GoogleCloudPlatform:pubsub-hp.

@pongad
Copy link
Contributor Author
pongad commented Jan 22, 2017

David's first pass is at 9dc57e8

I'm not sure why the review is not showing up here. @garrettjonesgoogle @davidtorres PTAL.

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling 0aea0fc on pongad:fix-iter into ** on GoogleCloudPlatform:pubsub-hp**.

@pongad
Copy link
Contributor Author
pongad commented Jan 23, 2017

David LGTMed in the linked PR. Still not sure why it's not here...

@pongad pongad merged commit 4e45df5 into googleapis:pubsub-hp Jan 24, 2017
@pongad pongad deleted the fix-iter branch January 24, 2017 01:17
pongad added a commit that referenced this pull request Jan 24, 2017
This is a follow up to
#1552 .

The linked PR has a race condition:
If the ExtensionJobs is added to the queue after the alarm is set up,
it is possible that the alarm would fire before the jobs are added.
Fixing this is easy, just set up the alarm after.
However, doing this consistently deadlocks the tests. Why?

The tests uses a fake executor.
In tests, time does not flow naturally, but is forced to increment,
usually by the executor's `advanceTime` method.
There is a race between the test thread advancing the time and
the mock server thread inserting more tasks to the fake executor.
If the tasks get inserted first, all is well.
Otherwise, `advanceTime` doesn't see the tasks,
and they do not get executed.
The fix is to check the "current time" every time a task is inserted.
If the task is inserted "in the past", we run the task immediately.
Doing this still deadlocks the tests. Why?

The fake executor needs to lock the tas
A31D
k queue when registering a task.
If the task is inserted in the past, it also needs to run the task.
Running the task might involve sending a requst to the mock server.
A GRPC thread on the mock server might handle the request by adding more
tasks to the executor.
The executor's queue is locked by the first thread,
resulting in a deadlock.
The fix is to lock the queue just long enough to retrieve a task,
then execute the task without the lock.
meltsufin pushed a commit that referenced this pull request Dec 22, 2025
🤖 I have created a release *beep* *boop*
---


## [3.16.1](https://togithub.com/googleapis/java-logging/compare/v3.16.0...v3.16.1) (2024-03-07)


### Bug Fixes

* **deps:** Update the Java code generator (gapic-generator-java) to 2.37.0 ([#1553](https://togithub.com/googleapis/java-logging/issues/1553)) ([15b05fc](https://togithub.com/googleapis/java-logging/commit/15b05fc4a8e6c4069414110b749525082821e509))


### Dependencies

* Update dependency com.google.cloud:sdk-platform-java-config to v3.27.0 ([#1552](https://togithub.com/googleapis/java-logging/issues/1552)) ([6c5464d](https://togithub.com/googleapis/java-logging/commit/6c5464d1c5a74962fcd459a1e03282747e148a44))

---
This PR was generated with [Release Please](https://togithub.com/googleapis/release-please). See [documentation](https://togithub.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0