forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract TransportRequestDeduplication from ShardStateAction (elastic#…
…37870) * Extracted the logic for master request duplication so it can be reused by the snapshotting logic * Removed custom listener used by `ShardStateAction` to not leak these into future users of this class * Changed semantics slightly to get rid of redundant instantiations of the composite listener * Relates elastic#37686
- Loading branch information
1 parent
6500b0c
commit a070b8a
Showing
7 changed files
with
241 additions
and
206 deletions.
There are no files selected for viewing
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
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
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
server/src/main/java/org/elasticsearch/transport/TransportRequestDeduplicator.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 Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.transport; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.common.util.concurrent.ConcurrentCollections; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.function.BiConsumer; | ||
|
||
/** | ||
* Deduplicator for {@link TransportRequest}s that keeps track of {@link TransportRequest}s that should | ||
* not be sent in parallel. | ||
* @param <T> Transport Request Class | ||
*/ | ||
public final class TransportRequestDeduplicator<T extends TransportRequest> { | ||
|
||
private final ConcurrentMap<T, CompositeListener> requests = ConcurrentCollections.newConcurrentMap(); | ||
|
||
/** | ||
* Ensures a given request not executed multiple times when another equal request is already in-flight. | ||
* If the request is not yet known to the deduplicator it will invoke the passed callback with an {@link ActionListener} | ||
* that must be completed by the caller when the request completes. Once that listener is completed the request will be removed from | ||
* the deduplicator's internal state. If the request is already known to the deduplicator it will keep | ||
* track of the given listener and invoke it when the listener passed to the callback on first invocation is completed. | ||
* @param request Request to deduplicate | ||
* @param listener Listener to invoke on request completion | ||
* @param callback Callback to be invoked with request and completion listener the first time the request is added to the deduplicator | ||
*/ | ||
public void executeOnce(T request, ActionListener<Void> listener, BiConsumer<T, ActionListener<Void>> callback) { | ||
ActionListener<Void> completionListener = requests.computeIfAbsent(request, CompositeListener::new).addListener(listener); | ||
if (completionListener != null) { | ||
callback.accept(request, completionListener); | ||
} | ||
} | ||
|
||
public int size() { | ||
return requests.size(); | ||
} | ||
|
||
private final class CompositeListener implements ActionListener<Void> { | ||
|
||
private final List<ActionListener<Void>> listeners = new ArrayList<>(); | ||
|
||
private final T request; | ||
|
||
private boolean isNotified; | ||
private Exception failure; | ||
|
||
CompositeListener(T request) { | ||
this.request = request; | ||
} | ||
|
||
CompositeListener addListener(ActionListener<Void> listener) { | ||
synchronized (this) { | ||
if (this.isNotified == false) { | ||
listeners.add(listener); | ||
return listeners.size() == 1 ? this : null; | ||
} | ||
} | ||
if (failure != null) { | ||
listener.onFailure(failure); | ||
} else { | ||
listener.onResponse(null); | ||
} | ||
return null; | ||
} | ||
|
||
private void onCompleted(Exception failure) { | ||
synchronized (this) { | ||
this.failure = failure; | ||
this.isNotified = true; | ||
} | ||
try { | ||
if (failure == null) { | ||
ActionListener.onResponse(listeners, null); | ||
} else { | ||
ActionListener.onFailure(listeners, failure); | ||
} | ||
} finally { | ||
requests.remove(request); | ||
} | ||
} | ||
|
||
@Override | ||
public void onResponse(final Void aVoid) { | ||
onCompleted(null); | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception failure) { | ||
onCompleted(failure); | ||
} | ||
} | ||
} |
Oops, something went wrong.