Skip to content
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

Add plumbing logic to invoke shard indexing pressure during write operation. #497

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.io.stream.Writeable;
import org.opensearch.index.ShardIndexingPressure;

import java.io.IOException;
import java.util.Collections;
Expand All @@ -40,6 +41,8 @@ public class CommonStatsFlags implements Writeable, Cloneable {
private String[] completionDataFields = null;
private boolean includeSegmentFileSizes = false;
private boolean includeUnloadedSegments = false;
private boolean includeAllShardIndexingPressureTrackers = false;
private boolean includeOnlyTopIndexingPressureMetrics = false;

/**
* @param flags flags to set. If no flags are supplied, default flags will be set.
Expand Down Expand Up @@ -67,6 +70,15 @@ public CommonStatsFlags(StreamInput in) throws IOException {
if (in.getVersion().onOrAfter(Version.V_7_2_0)) {
includeUnloadedSegments = in.readBoolean();
}
if (in.getVersion().onOrAfter(Version.V_7_10_2)) {
includeAllShardIndexingPressureTrackers = in.readBoolean();
includeOnlyTopIndexingPressureMetrics = in.readBoolean();
} else if (in.getVersion().onOrAfter(Version.V_7_9_0)) {
if (ShardIndexingPressure.isShardIndexingPressureAttributeEnabled()) {
includeAllShardIndexingPressureTrackers = in.readBoolean();
includeOnlyTopIndexingPressureMetrics = in.readBoolean();
}
}
}

@Override
Expand All @@ -85,6 +97,15 @@ public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().onOrAfter(Version.V_7_2_0)) {
out.writeBoolean(includeUnloadedSegments);
}
if (out.getVersion().onOrAfter(Version.V_7_10_2)) {
out.writeBoolean(includeAllShardIndexingPressureTrackers);
out.writeBoolean(includeOnlyTopIndexingPressureMetrics);
} else if (out.getVersion().onOrAfter(Version.V_7_9_0)) {
if (ShardIndexingPressure.isShardIndexingPressureAttributeEnabled()) {
out.writeBoolean(includeAllShardIndexingPressureTrackers);
out.writeBoolean(includeOnlyTopIndexingPressureMetrics);
}
}
}

/**
Expand All @@ -98,6 +119,8 @@ public CommonStatsFlags all() {
completionDataFields = null;
includeSegmentFileSizes = false;
includeUnloadedSegments = false;
includeAllShardIndexingPressureTrackers = false;
includeOnlyTopIndexingPressureMetrics = false;
return this;
}

Expand All @@ -112,6 +135,8 @@ public CommonStatsFlags clear() {
completionDataFields = null;
includeSegmentFileSizes = false;
includeUnloadedSegments = false;
includeAllShardIndexingPressureTrackers = false;
includeOnlyTopIndexingPressureMetrics = false;
return this;
}

Expand Down Expand Up @@ -185,10 +210,28 @@ public CommonStatsFlags includeUnloadedSegments(boolean includeUnloadedSegments)
return this;
}

public CommonStatsFlags includeAllShardIndexingPressureTrackers(boolean includeAllShardPressureTrackers) {
this.includeAllShardIndexingPressureTrackers = includeAllShardPressureTrackers;
return this;
}

public CommonStatsFlags includeOnlyTopIndexingPressureMetrics(boolean includeOnlyTopIndexingPressureMetrics) {
this.includeOnlyTopIndexingPressureMetrics = includeOnlyTopIndexingPressureMetrics;
return this;
}

public boolean includeUnloadedSegments() {
return this.includeUnloadedSegments;
}

public boolean includeAllShardIndexingPressureTrackers() {
return this.includeAllShardIndexingPressureTrackers;
}

public boolean includeOnlyTopIndexingPressureMetrics() {
return this.includeOnlyTopIndexingPressureMetrics;
}

public boolean includeSegmentFileSizes() {
return this.includeSegmentFileSizes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ public static IndexRequest getIndexWriteRequest(DocWriteRequest<?> docWriteReque
protected void doExecute(Task task, BulkRequest bulkRequest, ActionListener<BulkResponse> listener) {
final long indexingBytes = bulkRequest.ramBytesUsed();
final boolean isOnlySystem = isOnlySystem(bulkRequest, clusterService.state().metadata().getIndicesLookup(), systemIndices);
final Releasable releasable = indexingPressure.markCoordinatingOperationStarted(indexingBytes, isOnlySystem);
final Releasable releasable;
if (indexingPressure.isShardIndexingPressureEnabled() == false) {
releasable = indexingPressure.markCoordinatingOperationStarted(indexingBytes, isOnlySystem);
} else {
releasable = () -> {};
}
final ActionListener<BulkResponse> releasingListener = ActionListener.runBefore(listener, releasable::close);
final String executorName = isOnlySystem ? Names.SYSTEM_WRITE : Names.WRITE;
try {
Expand Down Expand Up @@ -548,7 +553,17 @@ protected void doRun() {
if (task != null) {
bulkShardRequest.setParentTask(nodeId, task.getId());
}
shardBulkAction.execute(bulkShardRequest, new ActionListener<BulkShardResponse>() {
// Add the shard level accounting for coordinating and supply the listener
final Releasable releasable;
if (indexingPressure.getShardIndexingPressure().isShardIndexingPressureEnabled()) {
final boolean isOnlySystem = isOnlySystem(bulkRequest, clusterService.state().metadata().getIndicesLookup(),
systemIndices);
releasable = indexingPressure.getShardIndexingPressure()
.markCoordinatingOperationStarted(shardId, bulkShardRequest.ramBytesUsed(), isOnlySystem);
} else {
releasable = () -> {};
}
shardBulkAction.execute(bulkShardRequest, ActionListener.runBefore(new ActionListener<BulkShardResponse>() {
@Override
public void onResponse(BulkShardResponse bulkShardResponse) {
for (BulkItemResponse bulkItemResponse : bulkShardResponse.getResponses()) {
Expand Down Expand Up @@ -581,7 +596,7 @@ private void finishHim() {
listener.onResponse(new BulkResponse(responses.toArray(new BulkItemResponse[responses.length()]),
buildTookInMillis(startTimeNanos)));
}
});
}, releasable::close));
}
bulkRequest = null; // allow memory for bulk request items to be reclaimed before all items have been completed
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,30 +288,46 @@ private void handleOperationRequest(final Request request, final TransportChanne
Releasable releasable = checkOperationLimits(request);
ActionListener<Response> listener =
ActionListener.runBefore(new ChannelActionListener<>(channel, actionName, request), releasable::close);
runReroutePhase(task, request, listener, false);
// Add the shard level accounting for primary and chain the listener
Releasable shardReleasable = checkShardOperationLimits(request);
ActionListener<Response> finalListener = ActionListener.runBefore(listener, shardReleasable::close);
runReroutePhase(task, request, finalListener, false);
}

protected Releasable checkOperationLimits(final Request request) {
return () -> {};
}

protected Releasable checkShardOperationLimits(final Request request) {
return () -> {};
}

protected void handlePrimaryRequest(final ConcreteShardRequest<Request> request, final TransportChannel channel, final Task task) {
Releasable releasable = checkPrimaryLimits(request.getRequest(), request.sentFromLocalReroute(),
request.localRerouteInitiatedByNodeClient());
ActionListener<Response> listener =
ActionListener.runBefore(new ChannelActionListener<>(channel, transportPrimaryAction, request), releasable::close);
// Add the shard level accounting for transport primary and chain the listener
Releasable shardReleasable = checkShardPrimaryLimits(request.getRequest(), request.sentFromLocalReroute(),
request.localRerouteInitiatedByNodeClient());
ActionListener<Response> finalListener = ActionListener.runBefore(listener, shardReleasable::close);

try {
new AsyncPrimaryAction(request, listener, (ReplicationTask) task).run();
new AsyncPrimaryAction(request, finalListener, (ReplicationTask) task).run();
} catch (RuntimeException e) {
listener.onFailure(e);
finalListener.onFailure(e);
}
}

protected Releasable checkPrimaryLimits(final Request request, boolean rerouteWasLocal, boolean localRerouteInitiatedByNodeClient) {
return () -> {};
}

protected Releasable checkShardPrimaryLimits(final Request request, boolean rerouteWasLocal,
boolean localRerouteInitiatedByNodeClient) {
return () -> {};
}

class AsyncPrimaryAction extends AbstractRunnable {
private final ActionListener<Response> onCompletionListener;
private final ReplicationTask replicationTask;
Expand Down Expand Up @@ -524,18 +540,24 @@ protected void handleReplicaRequest(final ConcreteReplicaRequest<ReplicaRequest>
Releasable releasable = checkReplicaLimits(replicaRequest.getRequest());
ActionListener<ReplicaResponse> listener =
ActionListener.runBefore(new ChannelActionListener<>(channel, transportReplicaAction, replicaRequest), releasable::close);

// Add the shard level accounting for replica and chain the listener
Releasable shardReleasable = checkShardReplicaLimits(replicaRequest.getRequest());
ActionListener<ReplicaResponse> finalListener = ActionListener.runBefore(listener, shardReleasable::close);
try {
new AsyncReplicaAction(replicaRequest, listener, (ReplicationTask) task).run();
new AsyncReplicaAction(replicaRequest, finalListener, (ReplicationTask) task).run();
} catch (RuntimeException e) {
listener.onFailure(e);
finalListener.onFailure(e);
}
}

protected Releasable checkReplicaLimits(final ReplicaRequest request) {
return () -> {};
}

protected Releasable checkShardReplicaLimits(final ReplicaRequest request) {
return () -> {};
}

public static class RetryOnReplicaException extends OpenSearchException {

public RetryOnReplicaException(ShardId shardId, String msg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.opensearch.common.lease.Releasable;
import org.opensearch.common.settings.Settings;
import org.opensearch.index.IndexingPressure;
import org.opensearch.index.ShardIndexingPressure;
import org.opensearch.index.engine.Engine;
import org.opensearch.index.mapper.MapperParsingException;
import org.opensearch.index.shard.IndexShard;
Expand Down Expand Up @@ -64,6 +65,7 @@ public abstract class TransportWriteAction<
> extends TransportReplicationAction<Request, ReplicaRequest, Response> {

protected final IndexingPressure indexingPressure;
private final ShardIndexingPressure shardIndexingPressure;
protected final SystemIndices systemIndices;

private final Function<IndexShard, String> executorFunction;
Expand All @@ -79,6 +81,7 @@ protected TransportWriteAction(Settings settings, String actionName, TransportSe
request, replicaRequest, ThreadPool.Names.SAME, true, forceExecutionOnPrimary);
this.executorFunction = executorFunction;
this.indexingPressure = indexingPressure;
this.shardIndexingPressure = indexingPressure.getShardIndexingPressure();
this.systemIndices = systemIndices;
}

Expand All @@ -88,7 +91,20 @@ protected String executor(IndexShard shard) {

@Override
protected Releasable checkOperationLimits(Request request) {
return indexingPressure.markPrimaryOperationStarted(primaryOperationSize(request), force(request));
if (indexingPressure.isShardIndexingPressureEnabled() == false) {
return indexingPressure.markPrimaryOperationStarted(primaryOperationSize(request), force(request));
} else {
return () -> {};
}
}

@Override
protected Releasable checkShardOperationLimits(Request request) {
if (shardIndexingPressure.isShardIndexingPressureEnabled()) {
return shardIndexingPressure.markPrimaryOperationStarted(request.shardId, primaryOperationSize(request), force(request));
} else {
return () -> {};
}
}

protected boolean force(ReplicatedWriteRequest<?> request) {
Expand All @@ -102,19 +118,46 @@ protected boolean isSystemShard(ShardId shardId) {

@Override
protected Releasable checkPrimaryLimits(Request request, boolean rerouteWasLocal, boolean localRerouteInitiatedByNodeClient) {
if (rerouteWasLocal) {
// If this primary request was received from a local reroute initiated by the node client, we
// must mark a new primary operation local to the coordinating node.
if (localRerouteInitiatedByNodeClient) {
return indexingPressure.markPrimaryOperationLocalToCoordinatingNodeStarted(primaryOperationSize(request));
if (indexingPressure.isShardIndexingPressureEnabled() == false) {
if (rerouteWasLocal) {
// If this primary request was received from a local reroute initiated by the node client, we
// must mark a new primary operation local to the coordinating node.
if (localRerouteInitiatedByNodeClient) {
return indexingPressure.markPrimaryOperationLocalToCoordinatingNodeStarted(primaryOperationSize(request));
} else {
return () -> {};
}
} else {
return () -> {};
// If this primary request was received directly from the network, we must mark a new primary
// operation. This happens if the write action skips the reroute step (ex: rsync) or during
// primary delegation, after the primary relocation hand-off.
return indexingPressure.markPrimaryOperationStarted(primaryOperationSize(request), force(request));
}
} else {
// If this primary request was received directly from the network, we must mark a new primary
// operation. This happens if the write action skips the reroute step (ex: rsync) or during
// primary delegation, after the primary relocation hand-off.
return indexingPressure.markPrimaryOperationStarted(primaryOperationSize(request), force(request));
return () -> {};
}
}

@Override
protected Releasable checkShardPrimaryLimits(Request request, boolean rerouteWasLocal, boolean localRerouteInitiatedByNodeClient) {
if (shardIndexingPressure.isShardIndexingPressureEnabled()) {
if (rerouteWasLocal) {
// If this primary request was received from a local reroute initiated by the node client, we
// must mark a new primary operation local to the coordinating node.
if (localRerouteInitiatedByNodeClient) {
return shardIndexingPressure.markPrimaryOperationLocalToCoordinatingNodeStarted(request.shardId,
primaryOperationSize(request));
} else {
return () -> {};
}
} else {
// If this primary request was received directly from the network, we must mark a new primary
// operation. This happens if the write action skips the reroute step (ex: rsync) or during
// primary delegation, after the primary relocation hand-off.
return shardIndexingPressure.markPrimaryOperationStarted(request.shardId, primaryOperationSize(request), force(request));
}
} else {
return () -> {};
}
}

Expand All @@ -124,7 +167,20 @@ protected long primaryOperationSize(Request request) {

@Override
protected Releasable checkReplicaLimits(ReplicaRequest request) {
return indexingPressure.markReplicaOperationStarted(replicaOperationSize(request), force(request));
if (indexingPressure.isShardIndexingPressureEnabled() == false) {
return indexingPressure.markReplicaOperationStarted(replicaOperationSize(request), force(request));
} else {
return () -> {};
}
}

@Override
protected Releasable checkShardReplicaLimits(ReplicaRequest request) {
if (shardIndexingPressure.isShardIndexingPressureEnabled()) {
return shardIndexingPressure.markReplicaOperationStarted(request.shardId, replicaOperationSize(request), force(request));
} else {
return () -> {};
}
Comment on lines -127 to +183
Copy link
Collaborator

@Bukhtawar Bukhtawar Apr 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the abstraction could be simplified. Essentially what we are doing here is putting the knowledge of the back pressure mechanism across the board forcing top layers to know the mechanism when ideally they should be agnostic. As you note here shardIndexingPressure.isShardIndexingPressureEnabled() that both shard indexing pressure and indexing pressure cannot mutually co-exist. I advise we simplify this and abstract this knowledge out in low level components

Also from the comment you noted you intended to keep "ShardIndexingPressure as a sub feature and is contained with IndexingPressure"
I am sorry I don't follow you completely based on this part of the code

Copy link
Member Author

@getsaurabh02 getsaurabh02 Apr 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree there was some scope of simplification here. I have made the required structural changes by introducing a top level construct for IndexingPressure as IndexingPressureService in the latest update #496. This largely allows the orchestration of the indexing pressure invocations from all the places in the TransportAction, thereby allowing the top layers to be agnostic of the low level implementation and feature-settings. I see with this change the amount of plumbing changes have reduced with mostly API changes, and one additional call in case of coordinating node. This is all covered in #496 now.

Note: We can update/close this PR shortly after #496 is merged as plumbing changes are covered in that now.

}

protected long replicaOperationSize(ReplicaRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.opensearch.common.component.AbstractLifecycleComponent;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Setting;
import org.opensearch.index.IndexingPressure;
import org.opensearch.common.settings.Setting.Property;
import org.opensearch.common.settings.Settings;
import org.opensearch.node.Node;
Expand Down Expand Up @@ -65,6 +66,8 @@ public class ClusterService extends AbstractLifecycleComponent {

private RerouteService rerouteService;

private IndexingPressure indexingPressure;

public ClusterService(Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool) {
this(settings, clusterSettings, new MasterService(settings, clusterSettings, threadPool),
new ClusterApplierService(Node.NODE_NAME_SETTING.get(settings), settings, clusterSettings, threadPool));
Expand Down Expand Up @@ -190,6 +193,22 @@ public MasterService getMasterService() {
return masterService;
}

/**
* Getter and Setter for Indexing Pressure, This method is added specifically for getting IndexingPressure
* instance in ODFE PA plugin via ClusterService. Indexing Pressure instances can be accessible only via
* Node and NodeService class but none of them are present in the createComponents signature of ES OSS Plugin
* interface.
* {@link org.opensearch.plugins.Plugin#createComponents}
*/

public void setIndexingPressure(IndexingPressure indexingPressure) {
this.indexingPressure = indexingPressure;
}

public IndexingPressure getIndexingPressure() {
return indexingPressure;
}

public ClusterApplierService getClusterApplierService() {
return clusterApplierService;
}
Expand Down
Loading