-
Notifications
You must be signed in to change notification settings - Fork 96
GH-661: [Flight] JDBC: Cache failed locations #662
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
Merged
Changes from all commits
Commits
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
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
55 changes: 55 additions & 0 deletions
55
...-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/utils/FlightClientCache.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,55 @@ | ||
| /* | ||
| * 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.arrow.driver.jdbc.client.utils; | ||
|
|
||
| import com.github.benmanes.caffeine.cache.Cache; | ||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import java.time.Duration; | ||
| import org.apache.arrow.util.VisibleForTesting; | ||
|
|
||
| /** | ||
| * A cache for Flight clients. | ||
| * | ||
| * <p>The intent is to avoid constantly recreating clients to the same locations. gRPC can multiplex | ||
| * multiple requests over a single TCP connection, and a cache would let us take advantage of that. | ||
| * | ||
| * <p>At the time being it only tracks whether a location is reachable or not. To actually cache | ||
| * clients, we would need a way to incorporate other connection parameters (authentication, etc.) | ||
| * into the cache key. | ||
| */ | ||
| public final class FlightClientCache { | ||
| @VisibleForTesting Cache<String, ClientCacheEntry> clientCache; | ||
|
|
||
| public FlightClientCache() { | ||
| this.clientCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(600)).build(); | ||
| } | ||
|
|
||
| public boolean isDud(String key) { | ||
| return clientCache.getIfPresent(key) != null; | ||
| } | ||
|
|
||
| public void markLocationAsDud(String key) { | ||
| clientCache.put(key, new ClientCacheEntry()); | ||
| } | ||
|
|
||
| public void markLocationAsReachable(String key) { | ||
| clientCache.invalidate(key); | ||
| } | ||
|
|
||
| /** A cache entry (empty because we only track reachability, see outer class docstring). */ | ||
| public static final class ClientCacheEntry {} | ||
| } |
71 changes: 71 additions & 0 deletions
71
...dbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/utils/FlightLocationQueue.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,71 @@ | ||
| /* | ||
| * 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.arrow.driver.jdbc.client.utils; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.Deque; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
| import org.apache.arrow.flight.Location; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
|
||
| /** | ||
| * A queue of Flight locations to connect to for an endpoint. | ||
| * | ||
| * <p>This helper class is intended to encapsulate the retry logic in a testable manner. | ||
| */ | ||
| public final class FlightLocationQueue implements Iterator<Location> { | ||
| private final Deque<Location> locations; | ||
| private final Deque<Location> badLocations; | ||
|
|
||
| /** | ||
| * Create a new queue. | ||
| * | ||
| * @param flightClientCache An optional cache used to sort previously unreachable locations to the | ||
| * end. | ||
| * @param locations The locations to try. | ||
| */ | ||
| public FlightLocationQueue( | ||
| @Nullable FlightClientCache flightClientCache, List<Location> locations) { | ||
| this.locations = new ArrayDeque<>(); | ||
| this.badLocations = new ArrayDeque<>(); | ||
|
|
||
| for (Location location : locations) { | ||
| if (flightClientCache != null && flightClientCache.isDud(location.toString())) { | ||
| this.badLocations.add(location); | ||
| } else { | ||
| this.locations.add(location); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| return !locations.isEmpty() || !badLocations.isEmpty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Location next() { | ||
| if (!locations.isEmpty()) { | ||
| return locations.pop(); | ||
| } else if (!badLocations.isEmpty()) { | ||
| return badLocations.pop(); | ||
| } | ||
| throw new NoSuchElementException(); | ||
| } | ||
| } |
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
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
51 changes: 51 additions & 0 deletions
51
...c-core/src/test/java/org/apache/arrow/driver/jdbc/client/utils/FlightClientCacheTest.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,51 @@ | ||
| /* | ||
| * 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.arrow.driver.jdbc.client.utils; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import org.apache.arrow.flight.Location; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class FlightClientCacheTest { | ||
| @Test | ||
| void basicOperation() { | ||
| FlightClientCache cache = new FlightClientCache(); | ||
|
|
||
| Location location1 = Location.forGrpcInsecure("localhost", 8080); | ||
| Location location2 = Location.forGrpcInsecure("localhost", 8081); | ||
|
|
||
| assertFalse(cache.isDud(location1.toString())); | ||
| assertFalse(cache.isDud(location2.toString())); | ||
|
|
||
| cache.markLocationAsReachable(location1.toString()); | ||
| assertFalse(cache.isDud(location1.toString())); | ||
| assertFalse(cache.isDud(location2.toString())); | ||
|
|
||
| cache.markLocationAsDud(location1.toString()); | ||
| assertTrue(cache.isDud(location1.toString())); | ||
| assertFalse(cache.isDud(location2.toString())); | ||
|
|
||
| cache.markLocationAsDud(location2.toString()); | ||
| assertTrue(cache.isDud(location1.toString())); | ||
| assertTrue(cache.isDud(location2.toString())); | ||
|
|
||
| cache.markLocationAsReachable(location1.toString()); | ||
| assertFalse(cache.isDud(location1.toString())); | ||
| assertTrue(cache.isDud(location2.toString())); | ||
| } | ||
| } |
Oops, something went wrong.
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.