-
Notifications
You must be signed in to change notification settings - Fork 615
fix(core): self-heal etcd meta watch on transport reconnect #3062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
imbajin
merged 3 commits into
apache:master
from
dpol1:fix/3036-meta-listener-reconnect
Jun 23, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...aph-server/hugegraph-test/src/main/java/org/apache/hugegraph/meta/EtcdMetaDriverTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with this | ||
| * work for additional information regarding copyright ownership. The ASF | ||
| * licenses this file to You under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance with the | ||
| * License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.hugegraph.meta; | ||
|
|
||
| import java.nio.charset.Charset; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import org.apache.hugegraph.testutil.Assert; | ||
| import org.junit.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import io.etcd.jetcd.ByteSequence; | ||
| import io.etcd.jetcd.Client; | ||
| import io.etcd.jetcd.Watch; | ||
| import io.etcd.jetcd.options.WatchOption; | ||
| import io.etcd.jetcd.watch.WatchResponse; | ||
|
|
||
| /** | ||
| * Unit tests for {@link EtcdMetaDriver}'s watch recovery (issue #3036). jetcd | ||
| * retries recoverable errors itself, so the driver must re-subscribe only on the | ||
| * terminal {@code onCompleted} close, not on every {@code onError}. A mock jetcd | ||
| * {@link Client} drives these paths through the package-private test | ||
| * constructor; no live etcd is needed. | ||
| */ | ||
| public class EtcdMetaDriverTest { | ||
|
|
||
| @Test | ||
| public void testListenReWatchesOnCompleted() { | ||
| Watch watch = Mockito.mock(Watch.class); | ||
| EtcdMetaDriver driver = newDriver(watch); | ||
|
|
||
| driver.listen("k", response -> { }); | ||
| // onCompleted is jetcd's terminal-close signal: the watcher is gone, so | ||
| // the driver must re-subscribe (a second watch() call). | ||
| captureListener(watch).onCompleted(); | ||
|
|
||
| Mockito.verify(watch, Mockito.timeout(2000).times(2)) | ||
| .watch(Mockito.any(ByteSequence.class), | ||
| Mockito.any(WatchOption.class), | ||
| Mockito.any(Watch.Listener.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testListenDoesNotReWatchOnError() { | ||
| Watch watch = Mockito.mock(Watch.class); | ||
| EtcdMetaDriver driver = newDriver(watch); | ||
|
|
||
| driver.listen("k", response -> { }); | ||
| // jetcd already reschedules the same watcher for recoverable errors; | ||
| // re-subscribing here would create a duplicate watch. Assert it does not. | ||
| captureListener(watch).onError(new RuntimeException("recoverable")); | ||
|
|
||
| Mockito.verify(watch, Mockito.after(500).times(1)) | ||
| .watch(Mockito.any(ByteSequence.class), | ||
| Mockito.any(WatchOption.class), | ||
| Mockito.any(Watch.Listener.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testListenPrefixReWatchPreservesKeyAndPrefix() { | ||
| Watch watch = Mockito.mock(Watch.class); | ||
| EtcdMetaDriver driver = newDriver(watch); | ||
|
|
||
| driver.listenPrefix("prefix", response -> { }); | ||
| captureListener(watch).onCompleted(); | ||
|
|
||
| ArgumentCaptor<ByteSequence> keyCaptor = | ||
| ArgumentCaptor.forClass(ByteSequence.class); | ||
| ArgumentCaptor<WatchOption> optionCaptor = | ||
| ArgumentCaptor.forClass(WatchOption.class); | ||
| Mockito.verify(watch, Mockito.timeout(2000).times(2)) | ||
| .watch(keyCaptor.capture(), optionCaptor.capture(), | ||
| Mockito.any(Watch.Listener.class)); | ||
|
|
||
| // The re-created watch must still target "prefix" with prefix semantics, | ||
| // not silently downgrade to an exact-key watch. | ||
| ByteSequence reWatchKey = keyCaptor.getAllValues().get(1); | ||
| WatchOption reWatchOption = optionCaptor.getAllValues().get(1); | ||
| Assert.assertEquals("prefix", | ||
| reWatchKey.toString(Charset.defaultCharset())); | ||
| Assert.assertTrue(reWatchOption.isPrefix()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReWatchRetriesWhenFirstAttemptThrows() { | ||
| Watch watch = Mockito.mock(Watch.class); | ||
| // Initial watch() succeeds, the first re-watch throws, the retry succeeds. | ||
| // A single failed re-watch must not abandon recovery permanently. | ||
| Mockito.when(watch.watch(Mockito.any(ByteSequence.class), | ||
| Mockito.any(WatchOption.class), | ||
| Mockito.any(Watch.Listener.class))) | ||
| .thenReturn(null) | ||
| .thenThrow(new RuntimeException("etcd still unreachable")) | ||
| .thenReturn(null); | ||
| EtcdMetaDriver driver = newDriver(watch); | ||
|
|
||
| driver.listen("k", response -> { }); | ||
| captureListener(watch).onCompleted(); | ||
|
|
||
| Mockito.verify(watch, Mockito.timeout(2000).times(3)) | ||
| .watch(Mockito.any(ByteSequence.class), | ||
| Mockito.any(WatchOption.class), | ||
| Mockito.any(Watch.Listener.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testListenDeliversEventsToConsumer() { | ||
| Watch watch = Mockito.mock(Watch.class); | ||
| AtomicReference<WatchResponse> received = new AtomicReference<>(); | ||
| EtcdMetaDriver driver = newDriver(watch); | ||
|
|
||
| driver.listen("k", received::set); | ||
| WatchResponse response = Mockito.mock(WatchResponse.class); | ||
| captureListener(watch).onNext(response); | ||
|
|
||
| Assert.assertSame(response, received.get()); | ||
| } | ||
|
|
||
| private static EtcdMetaDriver newDriver(Watch watch) { | ||
| Client client = Mockito.mock(Client.class); | ||
| Mockito.when(client.getWatchClient()).thenReturn(watch); | ||
| EtcdMetaDriver driver = new EtcdMetaDriver(client); | ||
| // No backoff in tests so the re-subscribe runs promptly. | ||
| driver.reWatchDelayMs = 0L; | ||
| return driver; | ||
| } | ||
|
|
||
| private static Watch.Listener captureListener(Watch watch) { | ||
| ArgumentCaptor<Watch.Listener> captor = | ||
| ArgumentCaptor.forClass(Watch.Listener.class); | ||
| Mockito.verify(watch).watch(Mockito.any(ByteSequence.class), | ||
| Mockito.any(WatchOption.class), | ||
| captor.capture()); | ||
| return captor.getValue(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.