-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework interface for accessing server-sent metadata during DoPut
- Loading branch information
Showing
9 changed files
with
338 additions
and
91 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
java/flight/src/main/java/org/apache/arrow/flight/AsyncPutListener.java
This file contains 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,63 @@ | ||
/* | ||
* 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.flight; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
/** | ||
* A handler for server-sent application metadata messages during a Flight DoPut operation. | ||
* | ||
* <p>To handle messages, create an instance of this class overriding {@link #onNext(PutResult)}. The other methods | ||
* should not be overridden. | ||
*/ | ||
public class AsyncPutListener implements FlightClient.PutListener { | ||
|
||
private CompletableFuture<Void> completed; | ||
|
||
public AsyncPutListener() { | ||
completed = new CompletableFuture<>(); | ||
} | ||
|
||
/** | ||
* Wait for the stream to finish on the server side. You must call this to be notified of any errors that may have | ||
* happened during the upload. | ||
*/ | ||
@Override | ||
public final void getResult() { | ||
try { | ||
completed.get(); | ||
} catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onNext(PutResult val) { | ||
} | ||
|
||
@Override | ||
public final void onError(Throwable t) { | ||
completed.completeExceptionally(t); | ||
} | ||
|
||
@Override | ||
public final void onCompleted() { | ||
completed.complete(null); | ||
} | ||
} |
This file contains 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
114 changes: 114 additions & 0 deletions
114
java/flight/src/main/java/org/apache/arrow/flight/SyncPutListener.java
This file contains 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,114 @@ | ||
/* | ||
* 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.flight; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.netty.buffer.ArrowBuf; | ||
|
||
/** | ||
* A listener for server-sent application metadata messages during a Flight DoPut. This class wraps the messages in a | ||
* synchronous interface. | ||
*/ | ||
public final class SyncPutListener implements FlightClient.PutListener, AutoCloseable { | ||
|
||
private final LinkedBlockingQueue<Object> queue; | ||
private final CompletableFuture<Void> completed; | ||
private static final Object DONE = new Object(); | ||
private static final Object DONE_WITH_EXCEPTION = new Object(); | ||
|
||
public SyncPutListener() { | ||
queue = new LinkedBlockingQueue<>(); | ||
completed = new CompletableFuture<>(); | ||
} | ||
|
||
private PutResult unwrap(Object queueItem) throws InterruptedException, ExecutionException { | ||
if (queueItem == DONE) { | ||
queue.put(queueItem); | ||
return null; | ||
} else if (queueItem == DONE_WITH_EXCEPTION) { | ||
queue.put(queueItem); | ||
completed.get(); | ||
} | ||
return (PutResult) queueItem; | ||
} | ||
|
||
/** | ||
* Get the next message from the server, blocking until it is available. | ||
* | ||
* @return The next message, or null if the server is done sending messages. The caller assumes ownership of the | ||
* metadata and must remember to close it. | ||
* @throws InterruptedException if interrupted while waiting. | ||
* @throws ExecutionException if the server sent an error, or if there was an internal error. | ||
*/ | ||
public PutResult read() throws InterruptedException, ExecutionException { | ||
return unwrap(queue.take()); | ||
} | ||
|
||
/** | ||
* Get the next message from the server, blocking for the specified amount of time until it is available. | ||
* | ||
* @return The next message, or null if the server is done sending messages or no message arrived before the timeout. | ||
* The caller assumes ownership of the metadata and must remember to close it. | ||
* @throws InterruptedException if interrupted while waiting. | ||
* @throws ExecutionException if the server sent an error, or if there was an internal error. | ||
*/ | ||
public PutResult poll(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException { | ||
return unwrap(queue.poll(timeout, unit)); | ||
} | ||
|
||
@Override | ||
public void getResult() { | ||
try { | ||
completed.get(); | ||
} catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onNext(PutResult val) { | ||
final ArrowBuf metadata = val.getApplicationMetadata(); | ||
metadata.getReferenceManager().retain(); | ||
queue.add(PutResult.metadata(metadata)); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
completed.completeExceptionally(t); | ||
queue.add(DONE_WITH_EXCEPTION); | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
completed.complete(null); | ||
queue.add(DONE); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
queue.forEach(o -> { | ||
if (o instanceof PutResult) { | ||
((PutResult) o).close(); | ||
} | ||
}); | ||
} | ||
} |
This file contains 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.