From 05571d4a08cc14367152154a1c2dc336a2d10845 Mon Sep 17 00:00:00 2001 From: Handa Wang Date: Wed, 27 Mar 2024 07:18:57 +0000 Subject: [PATCH] [Thread] remove queueing mechanism for NsdPublisher The queueing mechanism is not needed because b/323300118 is fixed. Bug: 331512403 Bug: 323300118 Test: atest CtsThreadNetworkTestCases Change-Id: Id7e868db69668494c1c7e3c473931a3087dc5755 --- .../android/server/thread/NsdPublisher.java | 68 ++----------------- 1 file changed, 6 insertions(+), 62 deletions(-) diff --git a/thread/service/java/com/android/server/thread/NsdPublisher.java b/thread/service/java/com/android/server/thread/NsdPublisher.java index 72e3980dc4..2c14f1d574 100644 --- a/thread/service/java/com/android/server/thread/NsdPublisher.java +++ b/thread/service/java/com/android/server/thread/NsdPublisher.java @@ -39,10 +39,8 @@ import com.android.server.thread.openthread.INsdStatusReceiver; import java.net.Inet6Address; import java.net.InetAddress; -import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; -import java.util.Deque; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -56,14 +54,6 @@ import java.util.concurrent.Executor; * *

All the data members of this class MUST be accessed in the {@code mHandler}'s Thread except * {@code mHandler} itself. - * - *

TODO: b/323300118 - Remove the following mechanism when the race condition in NsdManager is - * fixed. - * - *

There's always only one running registration job at any timepoint. All other pending jobs are - * queued in {@code mRegistrationJobs}. When a registration job is complete (i.e. the according - * method in {@link NsdManager.RegistrationListener} is called), it will start the next registration - * job in the queue. */ public final class NsdPublisher extends INsdPublisher.Stub { // TODO: b/321883491 - specify network for mDNS operations @@ -74,7 +64,6 @@ public final class NsdPublisher extends INsdPublisher.Stub { private final SparseArray mRegistrationListeners = new SparseArray<>(0); private final SparseArray mDiscoveryListeners = new SparseArray<>(0); private final SparseArray mServiceInfoListeners = new SparseArray<>(0); - private final Deque mRegistrationJobs = new ArrayDeque<>(); @VisibleForTesting public NsdPublisher(NsdManager nsdManager, Handler handler) { @@ -97,13 +86,9 @@ public final class NsdPublisher extends INsdPublisher.Stub { List txt, INsdStatusReceiver receiver, int listenerId) { - postRegistrationJob( - () -> { - NsdServiceInfo serviceInfo = - buildServiceInfoForService( - hostname, name, type, subTypeList, port, txt); - registerInternal(serviceInfo, receiver, listenerId, "service"); - }); + NsdServiceInfo serviceInfo = + buildServiceInfoForService(hostname, name, type, subTypeList, port, txt); + mHandler.post(() -> registerInternal(serviceInfo, receiver, listenerId, "service")); } private static NsdServiceInfo buildServiceInfoForService( @@ -132,11 +117,8 @@ public final class NsdPublisher extends INsdPublisher.Stub { @Override public void registerHost( String name, List addresses, INsdStatusReceiver receiver, int listenerId) { - postRegistrationJob( - () -> { - NsdServiceInfo serviceInfo = buildServiceInfoForHost(name, addresses); - registerInternal(serviceInfo, receiver, listenerId, "host"); - }); + NsdServiceInfo serviceInfo = buildServiceInfoForHost(name, addresses); + mHandler.post(() -> registerInternal(serviceInfo, receiver, listenerId, "host")); } private static NsdServiceInfo buildServiceInfoForHost( @@ -178,7 +160,7 @@ public final class NsdPublisher extends INsdPublisher.Stub { } public void unregister(INsdStatusReceiver receiver, int listenerId) { - postRegistrationJob(() -> unregisterInternal(receiver, listenerId)); + mHandler.post(() -> unregisterInternal(receiver, listenerId)); } public void unregisterInternal(INsdStatusReceiver receiver, int listenerId) { @@ -338,7 +320,6 @@ public final class NsdPublisher extends INsdPublisher.Stub { } } mRegistrationListeners.clear(); - mRegistrationJobs.clear(); } /** On ot-daemon died, reset. */ @@ -346,39 +327,6 @@ public final class NsdPublisher extends INsdPublisher.Stub { reset(); } - // TODO: b/323300118 - Remove this mechanism when the race condition in NsdManager is fixed. - /** Fetch the first job from the queue and run it. See the class doc for more details. */ - private void peekAndRun() { - if (mRegistrationJobs.isEmpty()) { - return; - } - Runnable job = mRegistrationJobs.getFirst(); - job.run(); - } - - // TODO: b/323300118 - Remove this mechanism when the race condition in NsdManager is fixed. - /** - * Pop the first job from the queue and run the next job. See the class doc for more details. - */ - private void popAndRunNext() { - if (mRegistrationJobs.isEmpty()) { - Log.i(TAG, "No registration jobs when trying to pop and run next."); - return; - } - mRegistrationJobs.removeFirst(); - peekAndRun(); - } - - private void postRegistrationJob(Runnable registrationJob) { - mHandler.post( - () -> { - mRegistrationJobs.addLast(registrationJob); - if (mRegistrationJobs.size() == 1) { - peekAndRun(); - } - }); - } - private final class RegistrationListener implements NsdManager.RegistrationListener { private final NsdServiceInfo mServiceInfo; private final int mListenerId; @@ -416,7 +364,6 @@ public final class NsdPublisher extends INsdPublisher.Stub { } catch (RemoteException ignored) { // do nothing if the client is dead } - popAndRunNext(); } @Override @@ -438,7 +385,6 @@ public final class NsdPublisher extends INsdPublisher.Stub { // do nothing if the client is dead } } - popAndRunNext(); } @Override @@ -456,7 +402,6 @@ public final class NsdPublisher extends INsdPublisher.Stub { } catch (RemoteException ignored) { // do nothing if the client is dead } - popAndRunNext(); } @Override @@ -477,7 +422,6 @@ public final class NsdPublisher extends INsdPublisher.Stub { } } mRegistrationListeners.remove(mListenerId); - popAndRunNext(); } }