forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract out PersistedStateStats and use PersistedState interface for…
… stats Signed-off-by: Aman Khare <[email protected]>
- Loading branch information
Aman Khare
committed
Oct 20, 2023
1 parent
0953f54
commit 9862244
Showing
18 changed files
with
237 additions
and
190 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
126 changes: 126 additions & 0 deletions
126
server/src/main/java/org/opensearch/cluster/coordination/PersistedStateStats.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,126 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.cluster.coordination; | ||
|
||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.common.io.stream.Writeable; | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* Persisted cluster state related stats. | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class PersistedStateStats implements Writeable, ToXContentObject { | ||
private String statsName; | ||
private AtomicLong totalTimeInMillis = new AtomicLong(0); | ||
private AtomicLong failedCount = new AtomicLong(0); | ||
private AtomicLong successCount = new AtomicLong(0); | ||
private Map<String, AtomicLong> extendedFields = new HashMap<>(); // keeping minimal extensibility | ||
|
||
public PersistedStateStats(String statsName) { | ||
this.statsName = statsName; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeVLong(successCount.get()); | ||
out.writeVLong(failedCount.get()); | ||
out.writeVLong(totalTimeInMillis.get()); | ||
if (extendedFields.size() > 0) { | ||
out.writeBoolean(true); | ||
out.writeVInt(extendedFields.size()); | ||
for (Map.Entry<String, AtomicLong> extendedField : extendedFields.entrySet()) { | ||
out.writeString(extendedField.getKey()); | ||
out.writeVLong(extendedField.getValue().get()); | ||
} | ||
} else { | ||
out.writeBoolean(false); | ||
} | ||
} | ||
|
||
public PersistedStateStats(StreamInput in) throws IOException { | ||
this.successCount = new AtomicLong(in.readVLong()); | ||
this.failedCount = new AtomicLong(in.readVLong()); | ||
this.totalTimeInMillis = new AtomicLong(in.readVLong()); | ||
if (in.readBoolean()) { | ||
int extendedFieldsSize = in.readVInt(); | ||
this.extendedFields = new HashMap<>(); | ||
for (int fieldNumber = 0; fieldNumber < extendedFieldsSize; fieldNumber++) { | ||
extendedFields.put(in.readString(), new AtomicLong(in.readVLong())); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(statsName); | ||
builder.field(Fields.UPDATE_COUNT, getSuccessCount()); | ||
builder.field(Fields.FAILED_COUNT, getFailedCount()); | ||
builder.field(Fields.TOTAL_TIME_IN_MILLIS, getTotalTimeInMillis()); | ||
if (extendedFields.size() > 0) { | ||
for (Map.Entry<String, AtomicLong> extendedField : extendedFields.entrySet()) { | ||
builder.field(extendedField.getKey(), extendedField.getValue().get()); | ||
} | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
public void stateUploadFailed() { | ||
failedCount.incrementAndGet(); | ||
} | ||
|
||
public void stateUploaded() { | ||
successCount.incrementAndGet(); | ||
} | ||
|
||
/** | ||
* Expects user to send time taken in milliseconds. | ||
* | ||
* @param timeTakenInUpload time taken in uploading the cluster state to remote | ||
*/ | ||
public void stateUploadTook(long timeTakenInUpload) { | ||
totalTimeInMillis.addAndGet(timeTakenInUpload); | ||
} | ||
|
||
public long getTotalTimeInMillis() { | ||
return totalTimeInMillis.get(); | ||
} | ||
|
||
public long getFailedCount() { | ||
return failedCount.get(); | ||
} | ||
|
||
public long getSuccessCount() { | ||
return successCount.get(); | ||
} | ||
|
||
protected void addToExtendedFields(String extendedField, AtomicLong extendedFieldValue) { | ||
this.extendedFields.put(extendedField, extendedFieldValue); | ||
} | ||
|
||
/** | ||
* Fields for parsing and toXContent | ||
* | ||
* @opensearch.internal | ||
*/ | ||
static final class Fields { | ||
static final String UPDATE_COUNT = "update_count"; | ||
static final String TOTAL_TIME_IN_MILLIS = "total_time_in_millis"; | ||
static final String FAILED_COUNT = "failed_count"; | ||
} | ||
} |
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
Oops, something went wrong.