Skip to content

Commit

Permalink
Subscribe to source job worker metrics in job master (#82)
Browse files Browse the repository at this point in the history
* Add mantis publish properties to docs

* Update MQL docs for using [*]

* Add myself as code owners

* Rule base auto scale should not scale down lower than min

* add comment

* Subscribe to source job drop metrics in job master

* add copy right, fix tests

* Address review comments. Add parameters to system params.

Co-authored-by: Calvin Cheung <[email protected]>
  • Loading branch information
calvin681 and calvin681 authored Dec 11, 2020
1 parent b6077a5 commit 022b7e6
Show file tree
Hide file tree
Showing 16 changed files with 941 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ public enum ScalingReason {
Clutch,
ClutchExperimental,
RPS,
JVMMemory
JVMMemory,
SourceJobDrop
}

public static class RollingCount {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ public Observable<Observable<MantisServerSentEvent>> call(Context context, Index
return sourceObs;
}

/**
* Use {@link io.mantisrx.runtime.parameter.SourceJobParameters.TargetInfo} instead.
*/
@Deprecated
public static class TargetInfo {

public String sourceJobName;
Expand Down Expand Up @@ -163,6 +167,10 @@ protected static List<TargetInfo> parseInputParameters(Context ctx) {
return parseTargetInfo(targetListStr);
}

/**
* Use {@link io.mantisrx.runtime.parameter.SourceJobParameters#parseTargetInfo(String)} instead.
*/
@Deprecated
protected static List<TargetInfo> parseTargetInfo(String targetListStr) {
List<TargetInfo> targetList = new ArrayList<TargetInfo>();
JsonObject requestObj = (JsonObject) parser.parse(targetListStr);
Expand Down Expand Up @@ -211,6 +219,10 @@ protected static List<TargetInfo> parseTargetInfo(String targetListStr) {
return targetList;
}

/**
* Use {@link io.mantisrx.runtime.parameter.SourceJobParameters.TargetInfoBuilder} instead.
*/
@Deprecated
public static class TargetInfoBuilder {

private String sourceJobName;
Expand Down Expand Up @@ -273,13 +285,16 @@ public TargetInfo build() {
}

/**
* Use {@link io.mantisrx.runtime.parameter.SourceJobParameters#enforceClientIdConsistency(List, String)} instead.
*
* Ensures that a list of TargetInfo contains a sane set of sourceJobName, ClientId pairs.
* TODO: Currently mutates the list, which isn't problematic here, but it would be prudent to clean this up.
*
* @param targets A List of TargetInfo for which to validate and correct clientId inconsistencies.
*
* @return The original List modified to have consistent clientIds.
*/
@Deprecated
public static List<TargetInfo> enforceClientIdConsistency(List<TargetInfo> targets, String defaultClientId) {

targets.sort(Comparator.comparing(t -> t.criterion));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import io.mantisrx.common.compression.CompressionUtils;
import io.mantisrx.mql.shaded.clojure.java.api.Clojure;
import io.mantisrx.mql.shaded.clojure.lang.IFn;
import com.netflix.spectator.api.BasicTag;
Expand Down Expand Up @@ -58,8 +59,12 @@ public class PushServerSse<T, S> extends PushServer<T, ServerSentEvent> {

private static final Logger logger = LoggerFactory.getLogger(PushServerSse.class);

public static final String PUSH_SERVER_METRIC_GROUP_NAME = "PushServerSse";
public static final String PUSH_SERVER_LEGACY_METRIC_GROUP_NAME = "ServerSentEventRequestHandler";
public static final String PROCESSED_COUNTER_METRIC_NAME = "processedCounter";
public static final String DROPPED_COUNTER_METRIC_NAME = "droppedCounter";
public static final String CLIENT_ID_TAG_NAME = "clientId";
public static final String SOCK_ADDR_TAG_NAME = "sockAddr";

private static IFn require = Clojure.var("io.mantisrx.mql.shaded.clojure.core", "require");

Expand Down Expand Up @@ -97,10 +102,10 @@ public PushServerSse(PushTrigger<T> trigger, ServerConfig<T> config,
}

private Metrics registerSseMetrics(String uniqueClientId, String socketAddrStr) {
final BasicTag clientIdTag = new BasicTag("clientId", Optional.ofNullable(uniqueClientId).orElse("none"));
final BasicTag sockAddrTag = new BasicTag("sockAddr", Optional.ofNullable(socketAddrStr).orElse("none"));
final BasicTag clientIdTag = new BasicTag(CLIENT_ID_TAG_NAME, Optional.ofNullable(uniqueClientId).orElse("none"));
final BasicTag sockAddrTag = new BasicTag(SOCK_ADDR_TAG_NAME, Optional.ofNullable(socketAddrStr).orElse("none"));

final String metricGroup = supportLegacyMetrics ? "ServerSentEventRequestHandler" : "PushServerSse";
final String metricGroup = supportLegacyMetrics ? PUSH_SERVER_LEGACY_METRIC_GROUP_NAME : PUSH_SERVER_METRIC_GROUP_NAME;
Metrics sseSinkMetrics = new Metrics.Builder()
.id(metricGroup, clientIdTag, sockAddrTag)
.addCounter(PROCESSED_COUNTER_METRIC_NAME)
Expand Down Expand Up @@ -156,7 +161,7 @@ public Observable<Void> handle(
predicateFunction = predicate.call(queryParameters);
}

byte[] delimiter = "$$$".getBytes();
byte[] delimiter = CompressionUtils.MANTIS_SSE_DELIMITER_BINARY;

if (queryParameters != null && !queryParameters.isEmpty()) {

Expand Down
3 changes: 2 additions & 1 deletion mantis-runtime/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/

test {
exclude 'io/mantisrx/runtime/**' // exclude remote tests that require ports
exclude 'io/mantisrx/runtime/executor/**' // exclude remote tests that require ports
exclude 'io/mantisrx/runtime/source/**' // exclude remote tests that require ports
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class ParameterUtils {
public static final String JOB_MASTER_AUTOSCALE_CONFIG_SYSTEM_PARAM = "mantis.jobmaster.autoscale.adaptive.config";
public static final String JOB_MASTER_CLUTCH_SYSTEM_PARAM = "mantis.jobmaster.clutch.config";
public static final String JOB_MASTER_CLUTCH_EXPERIMENTAL_PARAM = "mantis.jobmaster.clutch.experimental.enabled";
public static final String JOB_MASTER_AUTOSCALE_SOURCEJOB_METRIC_PARAM = "mantis.jobmaster.autoscale.sourcejob.metric.enabled";
public static final String JOB_MASTER_AUTOSCALE_SOURCEJOB_TARGET_PARAM = "mantis.jobmaster.autoscale.sourcejob.target";
public static final String JOB_MASTER_AUTOSCALE_SOURCEJOB_DROP_METRIC_PATTERNS_PARAM = "mantis.jobmaster.autoscale.sourcejob.dropMetricPatterns";
public static final String PER_STAGE_JVM_OPTS_FORMAT = "MANTIS_WORKER_JVM_OPTS_STAGE%d";
public static final String STAGE_CONCURRENCY = "mantis.stageConcurrency";
public static final int MAX_NUM_STAGES_FOR_JVM_OPTS_OVERRIDE = 5;
Expand Down Expand Up @@ -212,6 +215,32 @@ public class ParameterUtils {
.description("Delimiter for separating SSE data before compression")
.build();
systemParams.put(compressionDelimiter.getName(), compressionDelimiter);

ParameterDefinition<Boolean> autoscaleSourceJobMetricEnabled = new BooleanParameter()
.name(JOB_MASTER_AUTOSCALE_SOURCEJOB_METRIC_PARAM)
.validator(Validators.alwaysPass())
.defaultValue(false)
.description("Enable source job drop metrics to be used for autoscaling the 1st stage")
.build();
systemParams.put(autoscaleSourceJobMetricEnabled.getName(), autoscaleSourceJobMetricEnabled);

ParameterDefinition<String> autoscaleSourceJobTarget = new StringParameter()
.name(JOB_MASTER_AUTOSCALE_SOURCEJOB_TARGET_PARAM)
.validator(Validators.alwaysPass())
.defaultValue("{}")
.description("Json config to specify source job targets for autoscale metrics. This param is not needed if the 'target' param is already present. Example: {\"targets\": [{\"sourceJobName\":<jobName>, \"clientId\":<clientId>}]}")
.build();
systemParams.put(autoscaleSourceJobTarget.getName(), autoscaleSourceJobTarget);

ParameterDefinition<String> autoscaleSourceJobDropMetricPattern = new StringParameter()
.name(JOB_MASTER_AUTOSCALE_SOURCEJOB_DROP_METRIC_PATTERNS_PARAM)
.validator(Validators.alwaysPass())
.defaultValue("")
.description("Additional metrics pattern for source job drops. Comma separated list, supports dynamic client ID by using '_CLIENT_ID_' as a token. " +
"Each metric should be expressed in the same format as '" + JOB_MASTER_AUTOSCALE_METRIC_SYSTEM_PARAM + "'. " +
"Example: PushServerSse:clientId=_CLIENT_ID_:*::droppedCounter::MAX,ServerSentEventRequestHandler:clientId=_CLIENT_ID_:*::droppedCounter::MAX")
.build();
systemParams.put(autoscaleSourceJobDropMetricPattern.getName(), autoscaleSourceJobDropMetricPattern);
}

private ParameterUtils() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public class SinkParameter {
*/
public SinkParameter(String name, String value) throws UnsupportedEncodingException {
this.name = name;
this.value = value;
this.value = value == null ? "" : value;

encodedValue = URLEncoder.encode(value, "UTF-8");
encodedValue = URLEncoder.encode(this.value, "UTF-8");

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/*
* Copyright 2020 Netflix, Inc.
*
* Licensed 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 io.mantisrx.runtime.parameter;

import com.mantisrx.common.utils.MantisSSEConstants;
import io.mantisrx.runtime.Context;
import io.mantisrx.shaded.com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.mantisrx.shaded.com.fasterxml.jackson.annotation.JsonProperty;
import io.mantisrx.shaded.com.fasterxml.jackson.core.type.TypeReference;
import io.mantisrx.shaded.com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class SourceJobParameters {
public static final String MANTIS_SOURCEJOB_TARGET_KEY = "target";
public static final String MANTIS_SOURCEJOB_NAME_PARAM = "sourceJobName";
public static final String MANTIS_SOURCEJOB_CRITERION = "criterion";
public static final String MANTIS_SOURCEJOB_CLIENT_ID = "clientId";
public static final String MANTIS_SOURCEJOB_IS_BROADCAST_MODE = "isBroadcastMode";
private static final Logger log = LoggerFactory.getLogger(SourceJobParameters.class);
private static final ObjectMapper mapper = new ObjectMapper();


public static List<TargetInfo> parseInputParameters(Context ctx) {
String targetListStr = (String) ctx.getParameters()
.get(MANTIS_SOURCEJOB_TARGET_KEY, "{}");
return parseTargetInfo(targetListStr);
}

public static List<TargetInfo> parseTargetInfo(String targetListStr) {
List<TargetInfo> targetList = new ArrayList<TargetInfo>();

try {
Map<String, List<TargetInfo>> targets = mapper.readValue(targetListStr, new TypeReference<Map<String, List<TargetInfo>>>() {});
if (targets.get("targets") != null) {
return targets.get("targets");
}
} catch (Exception ex) {
log.error("Failed to parse target list: {}", targetListStr, ex);
}

return targetList;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class TargetInfo {

@JsonProperty(MANTIS_SOURCEJOB_NAME_PARAM) public String sourceJobName;
@JsonProperty(MANTIS_SOURCEJOB_CRITERION) public String criterion;
@JsonProperty(MANTIS_SOURCEJOB_CLIENT_ID) public String clientId;
@JsonProperty(MantisSSEConstants.SAMPLE) public int samplePerSec = -1;
@JsonProperty(MANTIS_SOURCEJOB_IS_BROADCAST_MODE) public boolean isBroadcastMode;
@JsonProperty(MantisSSEConstants.ENABLE_META_MESSAGES) public boolean enableMetaMessages;
@JsonProperty(MantisSSEConstants.MANTIS_ENABLE_COMPRESSION) public boolean enableCompressedBinary;
@JsonProperty(MantisSSEConstants.MANTIS_COMPRESSION_DELIMITER) public String delimiter;

public TargetInfo(String jobName,
String criterion,
String clientId,
int samplePerSec,
boolean isBroadcastMode,
boolean enableMetaMessages,
boolean enableCompressedBinary,
String delimiter) {
this.sourceJobName = jobName;
this.criterion = criterion;
this.clientId = clientId;
this.samplePerSec = samplePerSec;
this.isBroadcastMode = isBroadcastMode;
this.enableMetaMessages = enableMetaMessages;
this.enableCompressedBinary = enableCompressedBinary;
this.delimiter = delimiter;
}

public TargetInfo() {
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TargetInfo that = (TargetInfo) o;
return Objects.equals(sourceJobName, that.sourceJobName) &&
Objects.equals(criterion, that.criterion) &&
Objects.equals(clientId, that.clientId) &&
Objects.equals(samplePerSec, that.samplePerSec) &&
Objects.equals(isBroadcastMode, that.isBroadcastMode) &&
Objects.equals(enableMetaMessages, that.enableMetaMessages) &&
Objects.equals(enableCompressedBinary, that.enableCompressedBinary) &&
Objects.equals(delimiter, that.delimiter);
}

@Override
public int hashCode() {
return Objects.hash(sourceJobName, criterion, clientId, samplePerSec, isBroadcastMode, enableMetaMessages, enableCompressedBinary, delimiter);
}

@Override
public String toString() {
return "TargetInfo{" +
"sourceJobName=" + sourceJobName + "," +
"criterion=" + criterion + "," +
"clientId=" + clientId + "," +
"samplePerSec=" + samplePerSec + "," +
"isBroadcastMode=" + isBroadcastMode + "," +
"enableMetaMessages=" + enableMetaMessages + "," +
"enableCompressedBinary=" + enableCompressedBinary + "," +
"delimiter=" + delimiter + "," +
"}";
}
}

public static class TargetInfoBuilder {

private String sourceJobName;
private String criterion;
private String clientId;
private int samplePerSec = -1;
private boolean isBroadcastMode = false;
private boolean enableMetaMessages = false;
private boolean enableCompressedBinary = false;
private String delimiter = null;

public TargetInfoBuilder() {
}

public TargetInfoBuilder withSourceJobName(String srcJobName) {
this.sourceJobName = srcJobName;
return this;
}

public TargetInfoBuilder withQuery(String query) {
this.criterion = query;
return this;
}

public TargetInfoBuilder withSamplePerSec(int samplePerSec) {
this.samplePerSec = samplePerSec;
return this;
}

public TargetInfoBuilder withBroadCastMode() {
this.isBroadcastMode = true;
return this;
}

public TargetInfoBuilder withMetaMessagesEnabled() {
this.enableMetaMessages = true;
return this;
}


public TargetInfoBuilder withBinaryCompressionEnabled() {
this.enableCompressedBinary = true;
return this;
}

public TargetInfoBuilder withClientId(String clientId) {
this.clientId = clientId;
return this;
}

public TargetInfoBuilder withDelimiter(String delimiter) {
this.delimiter = delimiter;
return this;
}

public TargetInfo build() {
return new TargetInfo(
sourceJobName,
criterion,
clientId,
samplePerSec,
isBroadcastMode,
enableMetaMessages,
enableCompressedBinary,
delimiter);
}
}

/**
* Ensures that a list of TargetInfo contains a sane set of sourceJobName, ClientId pairs.
* TODO: Currently mutates the list, which isn't problematic here, but it would be prudent to clean this up.
*
* @param targets A List of TargetInfo for which to validate and correct clientId inconsistencies.
*
* @return The original List modified to have consistent clientIds.
*/
public static List<TargetInfo> enforceClientIdConsistency(List<TargetInfo> targets, String defaultClientId) {

targets.sort(Comparator.comparing(t -> t.criterion));
HashSet<Map.Entry<String, String>> connectionPairs = new HashSet<>(targets.size());

for (TargetInfo target : targets) {
if (target.clientId == null) {
target.clientId = defaultClientId;
}

Map.Entry<String, String> connectionPair = new AbstractMap.SimpleEntry(target.sourceJobName, target.clientId);
int attempts = 0;

while (connectionPairs.contains(connectionPair)) {
connectionPair = new AbstractMap.SimpleEntry(target.sourceJobName, target.clientId + "_" + ++attempts);
}

target.clientId = connectionPair.getValue();
connectionPairs.add(connectionPair);
}

return targets;
}
}
Loading

0 comments on commit 022b7e6

Please sign in to comment.